difference between two dates in Month

A

Ashish Sheth

Hi All,
In C#, How can I get the difference between two dates in number of months?
I tried to use the Substract method of the DateTime class and it is
giving me the difference in TimeSpan,From which I can get the duration
in days, hours and so.. but how can I get the difference in months?
Please reply ASAP. it's urgent.
 
L

Lasse Vågsæther Karlsen

Hi All,
In C#, How can I get the difference between two dates in number of
months? I tried to use the Substract method of the DateTime class and
it is giving me the difference in TimeSpan,From which I can get the
duration in days, hours and so.. but how can I get the difference in
months? Please reply ASAP. it's urgent.

Please explain how the difference in number of months can safely be
calculated from a TimeSpan value.

For instance, subtracting the last day of one month from the last day of
another month would for some be considered 1 month, but if the first date
was the 31st of january and the second date was the 28th of february, only
28 days have elapsed, is that a month ? How about from the 28th of february
to the 31st of march, that's 31 days. Is that one month too ? Even though
the second month there is bigger than the first ?

If you divide by 30 you get "1 year = 12 + 1/6 months". If you divide by 31
you get "1 year = 11 + 24/31 months". If you start with clever logic to
take the day of the month into account, you get months of varying sizes,
and TimeSpan has lost all references to the original dates so it can't
assume anything about the number of days it has stored.

The reason for years and months not appearing in the timespan class is
because of this problem. If you can define how to calculate the number of
months between two dates, just make a helper class that does just that.
 
G

Guest

Hi Ashish,

A TimeSpan represents only a time interval. Since the number of days in a
month is not constant, the TimeSpan structure doesn't have a value for diff
in months.

Days/30 will work though, assuming the number of days in a month is 30.

Eg:
DateTime firstDate = new DateTime(2004, 01, 20);
DateTime secondDate = new DateTime(2004, 04, 15);
TimeSpan diff = secondDate.Subtract(firstDate);
Console.WriteLine(diff.Days/30);

Will this do?

HTH,
Rakesh Rajan
 
J

Jay B. Harlow [MVP - Outlook]

Ashish,
In addition to the other comments, I would consider simply subtracting
DateTime.Months form DateTime.Months and adding that to DateTime.Years -
DateTime.Years multiplied by 12.

Something like (untested):

DateTime d1, d2;
int months = d1.Months - d2.Months;
int years = d1.Years - d2.Years;
int months += years * 12;

Hope this helps
Jay
 
G

Guest

Hi Jay - This won't work becaues what if the month of d1 is 01 and the month
of d2 is 02?
 
J

Jay B. Harlow [MVP - Outlook]

Gwenda,
Hi Jay - This won't work becaues what if the month of d1 is 01 and the
month
of d2 is 02?
Why not?

You do realize that you can either use System.Math.Abs to ensure the result
is positive or you can use a comparison & swap d1 for d2 to ensure that d1
is larger.

Of course you may want to have it return a negative just like
DateTime.Subtract does, which was what my sample shows!

Where it can be perceived it doesn't work is if you have Jan 31st & Feb 1st,
There is only a single day between them, however the algorithm I gave will
give a full month. Giving a full month may or may not be a real problem...
Hence my comment "I would consider simply"...

Another perceived problem is if you have Jan 1st & Jan 31st, is that
considered 1 month (its 30 days) or 0 months?

Obviously Ashish needs to better define what he is looking for...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top