两个日期之间月份的差异

本文翻译自:Difference in months between two dates

How to calculate the difference in months between two dates in C#? 如何计算C#中两个日期之间的月份差异?

Is there is equivalent of VB's DateDiff() method in C#. C#中是否有等效于VB的DateDiff()方法。 I need to find difference in months between two dates that are years apart. 我需要找出相隔数年的两个日期之间的月份差异。 The documentation says that I can use TimeSpan like: 文档说我可以像这样使用TimeSpan

TimeSpan ts = date1 - date2;

but this gives me data in Days. 但这给了我几天的数据。 I don't want to divide this number by 30 because not every month is 30 days and since the two operand values are quite apart from each other, I am afraid dividing by 30 might give me a wrong value. 我不想将这个数字除以30,因为不是每个月都有30天,并且由于两个操作数值彼此相距甚远,所以我怕除以30可能会给我一个错误的值。

Any suggestions? 有什么建议么?


#1楼

参考:https://stackoom.com/question/JSoT/两个日期之间月份的差异


#2楼

This is from my own library, will return the difference of months between two dates. 这来自我自己的库,将返回两个日期之间的月份差。

public static int MonthDiff(DateTime d1, DateTime d2)
{
    int retVal = 0;

    // Calculate the number of years represented and multiply by 12
    // Substract the month number from the total
    // Substract the difference of the second month and 12 from the total
    retVal = (d1.Year - d2.Year) * 12;
    retVal = retVal - d1.Month;
    retVal = retVal - (12 - d2.Month);

    return retVal;
}

#3楼

To be able to calculate the difference between 2 dates in months is a perfectly logical thing to do, and is needed in many business applications. 能够计算两个月中两个日期之间的差是一件很合理的事情,并且在许多业务应用程序中都需要。 The several coders here who have provided comments such as - what's the difference in months between "May 1,2010" and "June 16,2010, what's the difference in months between 31 December 2010 and 1 Jan 2011? -- have failed to understand the very basics of business applications. 此处提供评论的几位编码人员,例如-“ 2010年5月1日”和“ 2010年6月16日之间的月份有什么区别,2010年12月31日至2011年1月1日之间的月份有什么区别?”-无法理解业务应用程序的基础知识。

Here is the answer to the above 2 comments - The number of months between 1-may-2010 and 16-jun-2010 is 1 month, the number of months between 31-dec-2010 and 1-jan-2011 is 0. It would be very foolish to calculate them as 1.5 months and 1 second, as the coders above have suggested. 这是上述2条评论的答案-2010年5月1日至2010年6月16日之间的月数为1个月,2010年12月31日至2011年1月1日之间的月数为0。如上面的编码人员所建议的那样,将它们计算为1.5个月零1秒将是非常愚蠢的。

People who have worked on credit card, mortgage processing, tax processing, rent processing, monthly interest calculations and a vast variety of other business solutions would agree. 从事信用卡,抵押贷款处理,税收处理,租金处理,每月利息计算以及其他各种业务解决方案工作的人都会同意。

Problem is that such a function is not included in C# or VB.NET for that matter. 问题在于,C#或VB.NET中不包含此类功能。 Datediff only takes into account years or the month component, so is actually useless. Datediff仅考虑年份或月份组成部分,因此实际上是没有用的。

Here are some real-life examples of where you need to and correctly can calculate months: 以下是一些实际示例,您需要在这些示例中正确地计算月份:

You lived in a short-term rental from 18-feb to 23-aug. 您的短期租金为2月18日至23月8日。 How many months did you stay there? 你在那里呆了几个月? The answer is a simple - 6 months 答案很简单-6个月

You have a bank acount where interest is calculated and paid at the end of every month. 您有一个银行帐户,每个月底都会计算并支付利息。 You deposit money on 10-jun and take it out 29-oct (same year). 您在6月10日存入钱,然后取出10月29日(同年)。 How many months do you get interest for? 您对多少个月感兴趣? Very simple answer- 4 months (again the extra days do not matter) 很简单的答案-4个月(同样,多余的日子也没关系)

In business applications, most of the time, when you need to calculate months, it is because you need to know 'full' months based on how humans calculate time; 在业务应用程序中,大多数时候,当您需要计算月份时,这是因为您需要基于人类如何计算时间来了解“完整”月份。 not based on some abstract/irrelevant thoughts. 并非基于某些抽象/无关的想法。


#4楼

You can have a function something like this. 您可以拥有类似这样的功能。

For Example, from 2012/12/27 to 2012/12/29 becomes 3 days. 例如,从2012/12/27到2012/12/29变为3天。 Likewise, from 2012/12/15 to 2013/01/15 becomes 2 months, because up to 2013/01/14 it's 1 month. 同样,从2012/12/15到2013/01/15变成2个月,因为直到2013/01/14都是1个月。 from 15th it's 2nd month started. 从15日开始第二个月。

You can remove the "=" in the second if condition, if you do not want to include both days in the calculation. 如果您不想在计算中同时包括两天,则可以在第二个if条件中删除“ =”。 ie, from 2012/12/15 to 2013/01/15 is 1 month. 也就是说,从2012/12/15到2013/01/15是1个月。

public int GetMonths(DateTime startDate, DateTime endDate)
{
    if (startDate > endDate)
    {
        throw new Exception("Start Date is greater than the End Date");
    }

    int months = ((endDate.Year * 12) + endDate.Month) - ((startDate.Year * 12) + startDate.Month);

    if (endDate.Day >= startDate.Day)
    {
        months++;
    }

    return months;
}

#5楼

public static int PayableMonthsInDuration(DateTime StartDate, DateTime EndDate)
{
    int sy = StartDate.Year; int sm = StartDate.Month; int count = 0;
    do
    {
        count++;if ((sy == EndDate.Year) && (sm >= EndDate.Month)) { break; }
        sm++;if (sm == 13) { sm = 1; sy++; }
    } while ((EndDate.Year >= sy) || (EndDate.Month >= sm));
    return (count);
}

This solution is for Rental/subscription calculation, where difference doesn't means to be subtraction, it's meant to be the span in within those two dates. 此解决方案用于租赁/订阅计算,其中差异并不意味着相减,而是相差两个日期之内。


#6楼

This worked for what I needed it for. 这满足了我的需要。 The day of month didn't matter in my case because it always happens to be the last day of the month. 在我的情况下,月的一天并不重要,因为它总是恰好是该月的最后一天。

public static int MonthDiff(DateTime d1, DateTime d2){
    int retVal = 0;

    if (d1.Month

你可能感兴趣的:(两个日期之间月份的差异)