如何在C#中两个给定日期之间计算实际的月差(日历年不近似)?近似、日历、实际、两个

2023-09-03 07:40:16 作者:Echoes of the rainbow(岁月神偷)

例:下面给出了两个日期,终点总是大于或等于启动

  

开始= 2001年1月1日

     

完成= 2002年3月15日

因此​​,从2001年1月01至2002年2月底

  

个月= 12 + 2 = 14

有关2002年3月

  

15/30 = 0.5

     

因此​​总计为 14.5 个月的差别。

这很容易用手摸出但我怎么code是优雅?目前,我有很多的if else的组合和while循环,以达到我想要的,但我相信有更简单的解决方案在那里。

更新:的输出要求是 precise (不近似值),例如: 如果启动2001年1月01和2001年完成4月16日,输出应该是1 + 1 + 1 = 3(为一月,二月和三月)和16/31 =0.516个月,所以总为3.516。

另一个例子是,如果我开始在2001年7月5日,结束于2002年7月10日,输出应为11一个月到2002年6月底,和(31-5)/ 31 = 0.839和10/31 = 0.323个月,因此,总是11 + 0.839 + 0.323 = 12.162。

我延长乔希斯托多拉的code和 Hightechrider 的code:

 公共静态十进制GetMonthsInRange(此IDateRange thisDateRange)
{
    VAR开始= thisDateRange.Start;
    VAR完成= thisDateRange.Finish;

    VAR monthsApart = Math.Abs​​(12 *(start.Year  -  finish.Year)+ start.Month  -  finish.Month) -  1;

    十进制daysInStartMonth = DateTime.DaysInMonth(start.Year,start.Month);
    十进制daysInFinishMonth = DateTime.DaysInMonth(finish.Year,finish.Month);

    变种daysApartInStartMonth =(daysInStartMonth  -  start.Day + 1)/ daysInStartMonth;
    VAR daysApartInFinishMonth = finish.Day/daysInFinishMonth;

    返回monthsApart + daysApartInStartMonth + daysApartInFinishMonth;
}
 

解决方案

我给了一个 INT 的答案之前,然后意识到你问一个更precise回答。我累了,所以我删除了,上床睡觉。这么多的是,我无法入睡!出于某种原因,这个问题真的窃听我,我必须解决这个问题。所以,在这里你去...

 静态无效的主要(字串[] args)
{
    小数差异;

    的diff = monthDifference(新日期时间(2001年,1,1),新的日期时间(2002,3,15));
    Console.WriteLine(diff.ToString(N2)); //14.45

    的diff = monthDifference(新日期时间(2001年,1,1),新的日期时间(2001,4,16));
    Console.WriteLine(diff.ToString(N2)); //3.50

    的diff = monthDifference(新日期时间(2001,7,5),新日期时间(2002,7,10));
    Console.WriteLine(diff.ToString(N2)); //12.16

    Console.Read();
}

静态十进制monthDifference(DateTime的D1,D2的DateTime)
{
    如果(D1> D2)
    {
        日期时间举行= D1;
        D1 = D2;
        D2 =保持;
    }

    INT monthsApart = Math.Abs​​(12 *(d1.Year-d2.Year)+ d1.Month  -  d2.Month) -  1;
    十进制daysInMonth1 = DateTime.DaysInMonth(d1.Year,d1.Month);
    十进制daysInMonth2 = DateTime.DaysInMonth(d2.Year,d2.Month);

    十进制dayPercentage =((daysInMonth1  -  d1.Day)/ daysInMonth1)
                          +(d2.Day / daysInMonth2);
    返回monthsApart + dayPercentage;
}
 
Excel如何计算两个日期之间相差月数

现在我将有甜美的梦乡。晚安:)

Example: given two dates below, finish is always greater than or equal to start

start = 2001 Jan 01

finish = 2002 Mar 15

So from 2001 Jan 01 to the end of 2002 Feb

months = 12 + 2 = 14

For 2002 March

15/30 = 0.5

so grand total is 14.5 months difference.

It's very easy to work out by hand but how do I code it elegantly? At the moment I have the combination of a lot of if else and while loops to achieve what I want but I believe there are simpler solutions out there.

Update: the output needs to be precise (not approximation) for example: if start 2001 Jan 01 and finish 2001 Apr 16, the output should be 1 + 1 + 1= 3 (for Jan, Feb and Mar) and 16 / 31 = 0.516 month, so the total is 3.516.

Another example would be if I start on 2001 Jul 5 and finish on 2002 Jul 10, the output should be 11 month up to the end of June 2002, and (31-5)/31 = 0.839 and 10/31 = 0.323 months, so the total is 11 + 0.839 + 0.323 = 12.162.

I extended Josh Stodola's code and Hightechrider's code:

public static decimal GetMonthsInRange(this IDateRange thisDateRange)
{
    var start = thisDateRange.Start;
    var finish = thisDateRange.Finish;

    var monthsApart = Math.Abs(12*(start.Year - finish.Year) + start.Month - finish.Month) - 1;

    decimal daysInStartMonth = DateTime.DaysInMonth(start.Year, start.Month);
    decimal daysInFinishMonth = DateTime.DaysInMonth(finish.Year, finish.Month);

    var daysApartInStartMonth = (daysInStartMonth - start.Day + 1)/daysInStartMonth;
    var daysApartInFinishMonth = finish.Day/daysInFinishMonth;

    return monthsApart + daysApartInStartMonth + daysApartInFinishMonth;
}

解决方案

I gave an int answer before, and then realized what you asked for a more precise answer. I was tired, so I deleted and went to bed. So much for that, I was unable to fall asleep! For some reason, this question really bugged me, and I had to solve it. So here you go...

static void Main(string[] args)
{
    decimal diff;

    diff = monthDifference(new DateTime(2001, 1, 1), new DateTime(2002, 3, 15));
    Console.WriteLine(diff.ToString("n2")); //14.45

    diff = monthDifference(new DateTime(2001, 1, 1), new DateTime(2001, 4, 16));
    Console.WriteLine(diff.ToString("n2")); //3.50

    diff = monthDifference(new DateTime(2001, 7, 5), new DateTime(2002, 7, 10));
    Console.WriteLine(diff.ToString("n2")); //12.16

    Console.Read();
}

static decimal monthDifference(DateTime d1, DateTime d2)
{
    if (d1 > d2)
    {
        DateTime hold = d1;
        d1 = d2;
        d2 = hold;
    }

    int monthsApart = Math.Abs(12 * (d1.Year-d2.Year) + d1.Month - d2.Month) - 1;
    decimal daysInMonth1 = DateTime.DaysInMonth(d1.Year, d1.Month);
    decimal daysInMonth2 = DateTime.DaysInMonth(d2.Year, d2.Month);

    decimal dayPercentage = ((daysInMonth1 - d1.Day) / daysInMonth1)
                          + (d2.Day / daysInMonth2);
    return monthsApart + dayPercentage;
}

Now I shall have sweet dreams. Goodnight :)