.net - How to calculate actual months difference (calendar year not approximation) between two given dates in C#? -


Example: Two dates are given below, the end is always equal to or equal to the start

Start = 2001 January 01

ends = 2002 March 15

Then from 1 January 2001 to the end of 1 January 2002

months = 12 + 2 = 14

March 2002

15/30 = 0.5

There is a difference between the month of such a large total 14.5 .

It's very easy to work by hand but how do I code it beautifully? At the moment I have a lot of combinations if I do what I want to achieve, but I believe there is a simple solution.

Update: The output is required for example Precise (not approximate): if 2001 starts on January 01 and 2001 Ends April 16, production is 1 + 1 + 1 = 3 (for January, February and March) and 16/31 = 0.516 months, then the total is 3.516.

Another example would be if I started on July 1, 2001 and ended on July 10, 2002, production should be 11 months by 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 expanded Josh Stadola code and Highcaster 's code:

  Public static decimal GetMonthsInRange (this IDateRange ThisDateRange) {var start = thisDateRange.Start; Var finish = thisDateRange.Finish; Var MonthsApper = Math Abs (12 * (Initial Year - Finish. War) Start + Silent - Ended Death] - 1; StartMonth = DateTime in decimal days Dimension (start year, start, start.); Finish Simon = DateTime on Decimal Days End in the day (end year, end death.); Var daysApartInStartMonth = (start time of day - start.Day + 1) / day all startup; Var daysApartInFinishMonth = finish.Day/daysInInFinishMonth; Months of return more + daysApartinStartMonthth + daysApperInfinishmonths; }  

I had previously answered int , and then Realized that you had asked for a more accurate answer. I was tired, so I removed and went to bed. So much for this, I could not sleep! For some reason, this question actually damaged me, and I had to solve it. So here you can go ...

  Fixed zero main (string [] args) {decimal difference; Diff = monthDifference (new date time (2001, 1, 1), new date time (2002, 3, 15)); Console.WriteLine (diff.ToString ("n2"); //14.45 diff = monthDifference (new date time (2001, 1, 1), new date time (2001, 4, 16)); Console.WriteLine (diff.ToString ("n2"); //3.50 diff = monthDifference (new date time (2001, 7, 5), new date time (2002, 7, 10)); Console.WriteLine (diff.ToString ("n2"); //12.16 console Read (); } Fixed decimal month (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 days INMonth1 = DateTime DES INMONTH (D1. ERA, D1. MEN); Decimal day INMonth2 = DateTime Month in the day (D2 years, D2 death); Credit date = = ((Day 1 month - 1 day. Days) / Day 1 month) + (D.2 day / day day 2); Return month mostly + percentage of the day; }  

Now I have sweet dreams I am good night :)


Comments