0

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
  • 11,113
  • 66
  • 219
  • 392
  • There is a library called Nodatime which is better than DateTime at this sort of thing, there's a good answer [here](http://stackoverflow.com/a/15958122/969613) from the creator about how to do this in Nodatime. – JMK Oct 28 '13 at 10:21
  • A similar question was asked here on http://stackoverflow.com/questions/453208/how-can-i-calculate-the-age-of-a-person-in-year-month-days URL. – milan m Oct 28 '13 at 10:24
  • You should elaborate with a few examples. – H H Oct 28 '13 at 12:04
  • @HenkHolterman :If I have `66272` as a number of days ,then i want the number of years ,months and days in that number . – Anyname Donotcare Oct 28 '13 at 12:08
  • And how many years/days would that be? And for 45772 ? And for 32321, 9801, 1700 ? – H H Oct 28 '13 at 12:09
  • !! I ask cuz i don't know , i want a method to help me :) – Anyname Donotcare Oct 28 '13 at 12:11

3 Answers3

26

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
  • 80,725
  • 18
  • 167
  • 237
  • @James Thanks for this .... it give great result and solve my biggest Problem..... +1 – Imran Ali Khan Jun 14 '14 at 13:26
  • 2
    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
  • 1
    Wrong answer, please remove it – Ahmed_mag Sep 14 '21 at 10:22
  • @RowanBerry you clearly missed the [OPs comment](https://stackoverflow.com/questions/19632069/how-to-convert-number-of-days-to-years-months-and-days/19632218#comment29150121_19632133), and the fact I mentioned "based on your comments". It doesn't _have_ to be an estimate on it it's what the OP wanted... – James Oct 09 '22 at 21:57
  • @Ahmed_mag if you want to post the correct answer I'm sure the OP would be happy to accept. – James Oct 09 '22 at 22:04
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
  • 497
  • 1
  • 3
  • 16
  • Of course you can. It's just a matter of definitions. – H H Oct 28 '13 at 11:39
  • @HenkHolterman :I don't want it so exact , i will suggest that all years = 365 and all months = 30 so given number of days i want (years,months,days). i have number of days not two dates . – Anyname Donotcare Oct 28 '13 at 12:02
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,760
  • 14
  • 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