By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

difference between two dates in Month

P: n/a
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.

--
regards,
Ashish Sheth
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Ashish Sheth" <ea*****@gmail.com> wrote in
news:#D*************@tk2msftngp13.phx.gbl:
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.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2

P: n/a
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

"Ashish Sheth" wrote:
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.

--
regards,
Ashish Sheth

Nov 16 '05 #3

P: n/a
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

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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.

--
regards,
Ashish Sheth

Nov 16 '05 #4

P: n/a
Hi Jay - This won't work becaues what if the month of d1 is 01 and the month
of d2 is 02?

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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.

--
regards,
Ashish Sheth


Nov 16 '05 #5

P: n/a
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

"gwenda" <ni**@community.nospam> wrote in message
news:4D**********************************@microsof t.com... Hi Jay - This won't work becaues what if the month of d1 is 01 and the
month
of d2 is 02?

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
> 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.
>
> --
> regards,
> Ashish Sheth
>
>


Nov 16 '05 #6

P: 1
I ran into the same problem (which is how I found this thread) and, after googling and finding no real help, I came up with my own solution. Here is the exact code, including my overly-verbose comments, I used:

// first, we need to get the age as a TimeSpan
TimeSpan age = dateOfBirth - (new DateTime(1, 1, 1));
// then we can subtract that TimeSpan from the current date to return a DateTime object
// note that subtracting a DateTime from a DateTime returns a TimeSpan object, which is
// the reason for the first step above (we need a DateTime to get the years without
// having to go through some ugly calculation)
DateTime difference = DateTime.Now.Subtract(age);
// we'll need to subtract a year to account for that fact that we were forced to use the
// year 1 above in the new DateTime statement (throws an exception if you use 0)
sAge = difference.AddYears(-1).Year.ToString()

Obviously, to get the number of months requires only a slight modification. Something like:

int numMonths = difference.Month + (difference.AddYears(-1).Year * 12);

Hope that helps and PLEASE, if you see a problem with that logic, reply! :D
Mar 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.