470,849 Members | 1,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Finding difference of 2 dates in months using GregorianCalendar

I want to find difference between 2 dates in number of months using GregorianCalendar utility. I want it to work fine considering the year having 28, 29, 30 or 31 days in month. It should give me exact difference in all possible cases. Please help soon. Thanks in advance.
Mar 13 '08 #1
2 1800
Wagz
11
I want to find difference between 2 dates in number of months using GregorianCalendar utility. I want it to work fine considering the year having 28, 29, 30 or 31 days in month. It should give me exact difference in all possible cases. Please help soon. Thanks in advance.
I'd enter the dates as a Date type and simply subtract the two. This will give you the difference in days, hours, minutes, seconds, milliseconds. It doesn't however give you the difference in months. To do that I might do something like

date2.Year*12 + date2.Month - date1.Year*12 - date1.Month

Wagz
Mar 13 '08 #2
Plater
7,872 Expert 4TB
If you have two DateTime objects, when you subtract them you can typecast it as a TimeSpan. TimeSpan objects have a property where you can express it in the form of whole and fractional months.
Mar 13 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Hans Gruber | last post: by
26 posts views Thread by Frank | last post: by
7 posts views Thread by bryanilton | last post: by
6 posts views Thread by Ashish Sheth | last post: by
3 posts views Thread by bootzwiz | last post: by
4 posts views Thread by sniipe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.