3

I want exact Year, Month and Day elapsed between two dates.

DateTime startDate = new DateTime(1974, 8, 15);

DateTime endDate = DateTime.Now.ToLocalTime();

I wish to find Number of Years, Months and Days elapsed between the above two days using C#?

My Expected Output

Years: 68 Months: 10 Days: 23

I referred one of the post, in that they explained about only days Calculate difference between two dates (number of days)?

But I need all three - Year, Month and Day. Kindly assist me how to calculate...

Explanation for Duplicate: Already a question with same logic posted in Calculate Years, Months, weeks and Days, the answer provided in that question is too lengthy and in my question I asked only Year, Month and Date not Week. The Concept is same but the logic is different for calculating days comparing to that question, Here I got the answer in a very simplified manner. I satisfied in my answer.

Exact Duplicate:

Original Question: How to get difference between two dates in Year/Month/Week/Day? (Asked 7 Years ago)

Your Marked Question: Calculate Years, Months, weeks and Days (Asked 5 Years ago)

Community
  • 1
  • 1

1 Answers1

11

Interesting Question:

The Solution is

void Main()
{
    DateTime zeroTime = new DateTime(1, 1, 1);
    DateTime olddate = new DateTime(1947, 8,15);
    olddate.Dump();
    DateTime curdate = DateTime.Now.ToLocalTime();
    curdate.Dump();

    TimeSpan span = curdate - olddate;

    // because we start at year 1 for the Gregorian 
    // calendar, we must subtract a year here.

    int years = (zeroTime + span).Year - 1;
    int months = (zeroTime + span).Month - 1;
    int days = (zeroTime + span).Day;

    years.Dump();
    months.Dump();
    days.Dump();
}
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • 1
    The "solution" is very popular one, but it is clearly wrong as number of years/month depend on exact dates and definition of what OP is looking for. I.e. how long 30 days in month - 1 month 2 days or 1/1 or 1/0 or 0/30? – Alexei Levenkov Jul 06 '16 at 01:38
  • 1
    @AlexeiLevenkov The doubt is absolutely right. you test with **`DateTime olddate = DateTime.Now.AddDays(-29);`**. The result is Year: 0, Month: 0, Days: 30. It will work fine in all scenarios. – B.Balamanigandan Jul 06 '16 at 01:52
  • If my date starts with a given date then can it is necessary to subtract 1 from the year? – Abdullah Al Mamun Dec 26 '18 at 04:26
  • This is off by 1 day. Should be Day - 1. – Jerome Viveiros Jan 12 '21 at 14:01
  • 1
    @JeromeViveiros. I agree, but this is a matter of definition (whether or not you start counting at 0 or 1). – Mike Fuchs Jun 30 '21 at 16:36
  • B.Balamanigandan: Alexei is right. See my answer [here](https://stackoverflow.com/a/68200511/385995) for an extended explanation. I still think this code is the best approximation we can get, however. – Mike Fuchs Jun 30 '21 at 19:25
  • 1
    @MikeFuchs... To be fair, I'm using the code. It works but I don't see why the definition for day should be different to years or months. Honestly it doesn't make much of a difference as time goes by. Using it to show the time diff for overcoming addiction... I'm 8 years odd clean from meth and around six months since I gave up smoking... The days only matter right at the beginning. – Jerome Viveiros Jul 02 '21 at 07:01
  • 1
    @JeromeViveiros. Agreed, use cases which use this kind of output would rarely care too much about the exact number of days. Well done on overcoming addiction. – Mike Fuchs Jul 04 '21 at 10:18