-2

If i have Two dates then i get the difference between them in days Like this Post.


How to detail that in the following view :

convert the days to (number of years,number of months and the rest in the number of days)

Community
  • 1
  • 1
Anyname Donotcare
  • 10,649
  • 59
  • 212
  • 375

3 Answers3

24

There is no out-of-the-box solution for this. The problem is the data isn't "fixed" e.g. not all years are 365 days (366 on a leap year) and not every month can be assumed to be standard 30 days.

It's very difficult to calculate this sort of information without context. You have a duration in days, however, to accurately calculate the number of years/months you need to know exactly when these days fall i.e. on what month and of what year - this would allow you to determine the exact days in the month and/or year.


Based on your comments and the following conditions

  • 1 year = 365 days
  • 1 month = 30 days

Then the following code would do the job

DateTime startDate = new DateTime(2010, 1, 1);
DateTime endDate = new DateTime(2013, 1, 10);
var totalDays = (endDate - startDate).TotalDays;
var totalYears = Math.Truncate(totalDays / 365);
var totalMonths = Math.Truncate((totalDays % 365) / 30);
var remainingDays = Math.Truncate((totalDays % 365) % 30);
Console.WriteLine("Estimated duration is {0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays);
James
  • 77,877
  • 18
  • 158
  • 228
  • @James Thanks for this .... it give great result and solve my biggest Problem..... +1 – Imran Ali Khan Jun 14 '14 at 13:26
  • This code is horrifically inaccurate due to missing known data and is somehow the accepted answer. Every month has a set number of days except February which has either 28 or 29 days, so you calculate the leap year (year%4 == 0) and you start to see some accuracy. There is no reason why this needs to be an estimate – Rowan Berry Oct 22 '20 at 23:08
  • Wrong answer, please remove it – Ahmed_mag Sep 14 '21 at 10:22
4

You can't because it depends on the start date i.e. 30 days may be 1 month 1 day, or 1 month 2 days, or less than a month or 365 days will be less than a year if it's a leap year

Czeshirecat
  • 488
  • 1
  • 3
  • 14
4

As mentioned in previous answers, it is very difficult to work this out from just a number of days. There is problems with leap years, and the number of days in months. If you start with the original two datetimes you can use code similar to the following:

DateTime date1 = new DateTime(2010, 1, 18);
DateTime date2 = new DateTime(2013, 2, 22);

int oldMonth = date2.Month;
while (oldMonth == date2.Month)
{
     date1 = date1.AddDays(-1);
     date2 = date2.AddDays(-1);
}       

int years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0;

// getting number of years
while (date2.CompareTo(date1) >= 0)
{
     years++;
     date2 = date2.AddYears(-1);
}
date2 = date2.AddYears(1);
years--;


// getting number of months and days
oldMonth = date2.Month;
while (date2.CompareTo(date1) >= 0)
{
     days++;
     date2 = date2.AddDays(-1);
     if ((date2.CompareTo(date1) >= 0) && (oldMonth != date2.Month))
     {
          months++;
          days = 0;
          oldMonth = date2.Month;
     }
}
date2 = date2.AddDays(1);
days--;

TimeSpan difference = date2.Subtract(date1);

Console.WriteLine("Difference: " +
                    years.ToString() + " year(s)" +
                    ", " + months.ToString() + " month(s)" +
                    ", " + days.ToString() + " day(s)");

Output is:Difference: 3 year(s), 1 month(s), 4 day(s)

tomsullivan1989
  • 2,670
  • 13
  • 21
  • I can imagine this algorithm performing *really* sluggish given 2 dates which are relatively far apart. – James Oct 28 '13 at 10:31
  • 1
    It's Monday morning. I am performing _really_ sluggishly never mind the algorithm – tomsullivan1989 Oct 28 '13 at 10:37
  • 1
    I tried this code and calculated my age. It gave me correct results in 0.0045milliseconds. – milan m Oct 28 '13 at 10:42
  • I calculated this for dates 1000 years, 1 month and 4 days apart. It took 0.6867 milliseconds – tomsullivan1989 Oct 28 '13 at 11:12
  • 1
    @tomsullivan1989 you are no doubt on a relatively powerful development machine, unless the app is going to be running on a server which you have control over you need to think about client machine performance. I got stung with something like this on a project management module where we did similar calculations and it's fine for one or 2 simple operations...imagine trying performing the same one 100s of times...Probably doesn't apply here but I thought it was worth mentioning. – James Oct 28 '13 at 11:16