以下是我的代码。我只是得到两个日期之间的差异,但我想要在该日期和之间的那个月的名称。如何获得两个日期之间的月份集合?
public static int GetMonthsBetween(DateTime from, DateTime to)
{
if (from > to) return GetMonthsBetween(to, from);
var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));
if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
{
return monthDiff - 1;
}
else
{
return monthDiff;
}
}
你会重播吗?我认为这是Java,但我不确定。你的意思是使用'na'标签吗? – halfer
@halfer:据我所知现在是C#,至少这会解释“>”运算符进行比较。 –
您是否需要基于int的月份的名称,或者您是否需要计算,甚至是两者之间的距离,从DateTime还是到DateTime? – Andre