如何计算两个DateTimes之间的月数?

时间:2021-12-05 21:30:32

Requirements:

  • Calculate the number of months between two dates: receiveDate and dueDate.
  • 计算两个日期之间的月数:receiveDate和dueDate。

  • Both optimistic and pessimistic calculations are needed
  • 需要乐观和悲观的计算

Assumptions:

  • dueDate will always be the last day of the month.
  • dueDate将始终是该月的最后一天。

I've already figured out the pessimistic calculation (meaning a single day overdue counts as a whole month:

我已经发现了悲观的计算(意味着一天的过期数量整整一个月:

if(receiveDate > dueDate)
    receiveDate.Month - dueDate.Month + (receiveDate.Year - dueDate.Year) * 12;

Doing a search on the internet turned up several similar examples to confirm this.

在互联网上进行搜索发现了几个类似的例子来证实这一点。

Now my instincts tell me the optimistic calculation will just be the same minus one month but for some reason it just doesn't feel right. Am I on the right track or am I missing something?

现在我的直觉告诉我,乐观的计算只会减去一个月,但由于某种原因它只是感觉不对。我是在正确的轨道还是我错过了什么?

5 个解决方案

#1


You're right; if you're looking for the number of complete months between the two dates, subtracting 1 (assuming the receiveDate doesn't fall on the last day of the month, in which case you will have a remainder of 0 days either way) will get you your answer.

你是对的;如果你正在寻找两个日期之间完整月份的数量,减去1(假设receiveDate不会落在当月的最后一天,在这种情况下,你将有剩余的0天)你的回答。

#2


If you don't need to keep days of month in your calculus I think it's the way to go.

如果你不需要在你的微积分中保留几个月的日子,我认为这是要走的路。

#3


Your formula calculates the number of months between the first of the receivedDate's month to the first of the dueDate's month. As most time elements in TimeSpan are expressed as TotalXXX, it seems weird that they left out a TotalMonths and a TotalYears.

您的公式计算收到的月份的第一个月到dueDate月份的第一个月之间的月数。由于TimeSpan中的大多数时间元素都表示为TotalXXX,因此他们遗漏了TotalMonths和TotalYears似乎很奇怪。

I think it's because there aren't a fixed number of days from month to month, so it's hard to know what makes most sense in terms of how to express the fractional remainder.

我认为这是因为每个月都没有固定的天数,因此很难知道在如何表达小数余数方面最有意义的是什么。

My formula is this...

我的公式是这个......


int nMonthDiff_FirstToFirst = DateTime.Now.Month - testDate.Month + ((DateTime.Now.Year - testDate.Year)* 12);

double dMonthDiff = (double)nMonthDiff_FirstToFirst + (DateTime.Now - testDate.AddMonths(nMonthDiff_FirstToFirst)).TotalDays / (double)DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

So what I'm doing is basically getting the month difference like you are (first of the month to first of the month) then I project my testDate into the future by the month difference. Then with that TimeSpan I get the TotalDays and divide that by the number of days in that month. Thus I'm representing the fractional portion in terms of remaining days of the month.

所以我正在做的基本上就是像你这样的月份差异(本月的第一个月到本月的第一个月),然后按月差异将我的testDate投射到未来。然后使用TimeSpan,我获得TotalDays并将其除以该月的天数。因此,我代表了月份剩余天数的小数部分。

So if you were going May 5st 2012 -> June 3rd 2012, your formula would return 1 for month difference when a full month hasn't passed yet. In my formula the TotalDays of the projected date would yield a negative fractional number of days, and when added to the 'first to first' month difference, it would take it in as needed.

因此,如果你要去2012年5月5日 - > 2012年6月3日,那么当整整一个月尚未过去时,你的公式会为月份差异返回1。在我的公式中,预计日期的TotalDays将产生负的小数天数,并且当添加到“第一个到第一个”月份差异时,它将根据需要将其带入。

#4


I am doing this extra check to get precise amount of months:

我正在做这个额外的检查以获得精确的月数:

numberOfMonths = receiveDate.Month - dueDate.Month + (receiveDate.Year - dueDate.Year) * 12; if (receiveDate.Day > dueDate.Day) numberOfMonths--;

numberOfMonths = receiveDate.Month - dueDate.Month +(receiveDate.Year - dueDate.Year)* 12; if(receiveDate.Day> dueDate.Day)numberOfMonths--;

#5


int GetMonthsCount(DateTime dtStart, DateTime dtEnd)
{
    return (int)Math.Round((dtEnd - dtStart).TotalMonths);
}

#1


You're right; if you're looking for the number of complete months between the two dates, subtracting 1 (assuming the receiveDate doesn't fall on the last day of the month, in which case you will have a remainder of 0 days either way) will get you your answer.

你是对的;如果你正在寻找两个日期之间完整月份的数量,减去1(假设receiveDate不会落在当月的最后一天,在这种情况下,你将有剩余的0天)你的回答。

#2


If you don't need to keep days of month in your calculus I think it's the way to go.

如果你不需要在你的微积分中保留几个月的日子,我认为这是要走的路。

#3


Your formula calculates the number of months between the first of the receivedDate's month to the first of the dueDate's month. As most time elements in TimeSpan are expressed as TotalXXX, it seems weird that they left out a TotalMonths and a TotalYears.

您的公式计算收到的月份的第一个月到dueDate月份的第一个月之间的月数。由于TimeSpan中的大多数时间元素都表示为TotalXXX,因此他们遗漏了TotalMonths和TotalYears似乎很奇怪。

I think it's because there aren't a fixed number of days from month to month, so it's hard to know what makes most sense in terms of how to express the fractional remainder.

我认为这是因为每个月都没有固定的天数,因此很难知道在如何表达小数余数方面最有意义的是什么。

My formula is this...

我的公式是这个......


int nMonthDiff_FirstToFirst = DateTime.Now.Month - testDate.Month + ((DateTime.Now.Year - testDate.Year)* 12);

double dMonthDiff = (double)nMonthDiff_FirstToFirst + (DateTime.Now - testDate.AddMonths(nMonthDiff_FirstToFirst)).TotalDays / (double)DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

So what I'm doing is basically getting the month difference like you are (first of the month to first of the month) then I project my testDate into the future by the month difference. Then with that TimeSpan I get the TotalDays and divide that by the number of days in that month. Thus I'm representing the fractional portion in terms of remaining days of the month.

所以我正在做的基本上就是像你这样的月份差异(本月的第一个月到本月的第一个月),然后按月差异将我的testDate投射到未来。然后使用TimeSpan,我获得TotalDays并将其除以该月的天数。因此,我代表了月份剩余天数的小数部分。

So if you were going May 5st 2012 -> June 3rd 2012, your formula would return 1 for month difference when a full month hasn't passed yet. In my formula the TotalDays of the projected date would yield a negative fractional number of days, and when added to the 'first to first' month difference, it would take it in as needed.

因此,如果你要去2012年5月5日 - > 2012年6月3日,那么当整整一个月尚未过去时,你的公式会为月份差异返回1。在我的公式中,预计日期的TotalDays将产生负的小数天数,并且当添加到“第一个到第一个”月份差异时,它将根据需要将其带入。

#4


I am doing this extra check to get precise amount of months:

我正在做这个额外的检查以获得精确的月数:

numberOfMonths = receiveDate.Month - dueDate.Month + (receiveDate.Year - dueDate.Year) * 12; if (receiveDate.Day > dueDate.Day) numberOfMonths--;

numberOfMonths = receiveDate.Month - dueDate.Month +(receiveDate.Year - dueDate.Year)* 12; if(receiveDate.Day> dueDate.Day)numberOfMonths--;

#5


int GetMonthsCount(DateTime dtStart, DateTime dtEnd)
{
    return (int)Math.Round((dtEnd - dtStart).TotalMonths);
}