0

Possible Duplicate:
Calculate Years, Months, weeks and Days

How do I calculate difference in years and months given a start and end date?

Hi

Working on a c# 2.0 project and I need to calculate the difference in years and months given the start and end date

I was thinking of a function like

        public void CalculateYearsAndMonths(DateTime startDate,
                                            DateTime endDate,
                                            out int years,
                                            out int months)
        {
            int years=0;
            int months=0;

            //????
        }

any suggestions?

Community
  • 1
  • 1
user9969
  • 15,632
  • 39
  • 107
  • 175
  • 1
    Related : http://stackoverflow.com/questions/3286461/how-to-calculate-actual-months-difference-calendar-year-not-approximation-betwe – Saurabh Gokhale Jul 14 '11 at 18:26

5 Answers5

2

This code works even when number of days between two dates are 365 days. E.g. 29th February 1972 and 28. February 1973, then it will return 0 years and 11 months.

if (endDate < startDate)
{
    DateTime temp = endDate;
    endDate = startDate;
    startDate = temp;
}

years = endDate.Year - startDate.Year;
months = endDate.Month - startDate.Month;
int days = endDate.Day - startDate.Day;
if (days < 0) months--;
if (months < 0)
{
    months += 12;
    years--;
}

By the way, you need to remove the two lines to make the out parameters work correctly:

int years=0;
int months=0;
Casperah
  • 4,504
  • 1
  • 19
  • 13
1

I think the number of the months is ambiguous as each month get the different number of days. However, if based on DateTime.MinValue, you can use like:

        TimeSpan difference = endDate - startDate;

        DateTime age = DateTime.MinValue + difference;

        // Min value is 01/01/0001

        int ageInYears = age.Year - 1;
        int ageInMonths = age.Month - 1;
        int ageInDays = age.Day - 1;

        Console.WriteLine("{0}, {1}, {2}", ageInYears, ageInMonths, ageInDays); 
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • Hi thanks for your reply. could you tell me why -1?Trying to understand your logic.thanks – user9969 Jul 14 '11 at 19:13
  • As you can see the comment, Min value starts from 01/01/0001. It should be treated 0/0/0 to calculate the total. Instead of DateTime.MinValue, you can use `new DateTime(0,0,0)`, where arguments are ordered as year, month and day, and you don't need to subtract the 1. – Jin-Wook Chung Jul 14 '11 at 19:30
0
int years = endDate.Year - startDate.Year;
int months = endDate.Month - startDate.Month;

You can try the above, should work.

Jethro
  • 5,896
  • 3
  • 23
  • 24
0

This depends on how you want to count months and years. Do you care about boundaries crossed (e.g. Jan 25 to Feb 1 = one month OR Dec 30 to Jan 1 = 1 year) or about the number of average size months/years (e.g. 29 days in month or 365 days in year) between two dates?

The TimeSpan result you can get by subtracting dates will give you all the fixed size time periods (e.g. days, minutes)

See this question for some details around why months and years can be hard.

Community
  • 1
  • 1
Christoph
  • 4,251
  • 3
  • 24
  • 38
  • I see.Not sure to be honest.If I didnt care about boundaries crossed how would you do it? – user9969 Jul 14 '11 at 18:36
  • Which are you interested in boundaries crossed or average periods? – Christoph Jul 14 '11 at 18:43
  • BTW - not sure I get the down vote - working toward an answer getting more information on the question as needed. – Christoph Jul 14 '11 at 18:45
  • @Christopherous - I suspect the reason you recieved a down vote is because the author of the question simply wants the number of years and the number of months between two specfic dates. So simply subtracting the month and year from one another gets you that result. – Security Hound Jul 14 '11 at 19:02
  • i didnt downvote.If I did it was by mistake!! sorry – user9969 Jul 14 '11 at 19:02
0

Something like this?

        var end = endDate.Year + endDate.Month / 12.0;
        var start = startDate.Year + startDate.Month / 12.0;

        var years = (int)(end - start);
        var months = (end - start) * 12;

Not sure if you wanted to round the months...

Hope this helps,

John

JohnD
  • 14,327
  • 4
  • 40
  • 53