Finding difference of 2 dates in months using GregorianCalendar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shamim1983
    New Member
    • Mar 2008
    • 1

    Finding difference of 2 dates in months using GregorianCalendar

    I want to find difference between 2 dates in number of months using GregorianCalend ar 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.
  • Wagz
    New Member
    • Feb 2008
    • 11

    #2
    Originally posted by Shamim1983
    I want to find difference between 2 dates in number of months using GregorianCalend ar 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

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      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.

      Comment

      Working...