Google Answers Logo
View Question
 
Q: What is the EXACT differences between 2 given dates? ( No Answer,   6 Comments )
Question  
Subject: What is the EXACT differences between 2 given dates?
Category: Computers > Programming
Asked by: jm86-ga
List Price: $35.00
Posted: 21 Nov 2006 20:22 PST
Expires: 25 Nov 2006 18:53 PST
Question ID: 784696
What is the EXACT differences between the 2 given dates?
  For an example with just the date part, given 11/9/2005 and
11/21/2006, the return should be 1 year 0 month 12 days.

I would like the solution in:
  - C# or VB.NET code that takes full advantage of .NET fuctions as
much as possible such as DaysInMonths if appropriate
  - DateTimes Passed in via new constructor
  - Have the following return properties: Years, Months, Days, Hours,
Minutes, Seconds
 - Support localization (through .NET should taken care of this)
 - Efficient, Easy to read, Commented
 - Calling a calculator or use of external programs is not an acceptable answer

I hope I am not asking too much here. It should be an easy problem to figure it out.
Answer  
There is no answer at this time.

Comments  
Subject: Re: What is the EXACT differences between 2 given dates?
From: frankcorrao-ga on 22 Nov 2006 07:08 PST
 
Unless .net/windows has some built in date aritmetic functions, this
sort of thing is usually handled by converting the dates to seconds. 
Once you then have the differences in seconds, convert that back into
whatever format you need.

The problem is that converting the seconds difference into a
Years/months/days difference is dependent upon the original date
interval.  What I mean is that even if dates a and date b differ by s
seconds, and date c and date d also differ by s seconds, the answers
will not necessarily be the same because the length of months differ.
Subject: Re: What is the EXACT differences between 2 given dates?
From: mike76-ga on 22 Nov 2006 11:17 PST
 
DateTime date1 = new DateTime(2005, 11, 9);
DateTime date2 = new DateTime(2006, 11, 21);

TimeSpan t = ((TimeSpan)date2.Subtract(date1));


You can get the difference of the range for any interval using the
propeties of the TimeSpan object. For example to get the number of
seconds use t.TotalSeconds.
Subject: Re: What is the EXACT differences between 2 given dates?
From: jm86-ga on 22 Nov 2006 11:46 PST
 
Thank you for your comments through. Timespan has limitations since it
doesn't scale up to TotalYears and so forth.
Subject: Re: What is the EXACT differences between 2 given dates?
From: jm86-ga on 22 Nov 2006 11:48 PST
 
Also, getting TotalSeconds doesn't get "exact" differences since you
have to assume the days in the month and the the days in the year and
so forth. So you will be getting the "average" but not "exact"
Subject: Re: What is the EXACT differences between 2 given dates?
From: monsters-ga on 24 Nov 2006 07:26 PST
 
Is there a reason you can't just use a method like this?

static double GetYears(System.TimeSpan ts)
        {
//return high precision year, eg 12.34523453453
            return Convert.ToDouble(ts.Days) / 365;
        }
Subject: Re: What is the EXACT differences between 2 given dates?
From: jm86-ga on 25 Nov 2006 11:14 PST
 
Thanks again for your comment but that doesn't work as well because
then you have to assume how many days in the month and each month may
have different number of days. So in the end the differences will not
be "exact"

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy