Difference between two dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sniipe@gmail.com

    Difference between two dates

    Hi!

    I am new in Python, so I count for your help. I need to get difference
    in months between two dates. How to do it in python? I am substracting
    two dates, for example date1 - date2 and I got result in days, how to
    change it?

    Best regards
  • =?iso-8859-1?q?C=E9dric_Lucantis?=

    #2
    Re: Difference between two dates

    Le Tuesday 24 June 2008 12:11:03 sniipe@gmail.co m, vous avez écrit :
    Hi!
    >
    I am new in Python, so I count for your help. I need to get difference
    in months between two dates. How to do it in python? I am substracting
    two dates, for example date1 - date2 and I got result in days, how to
    change it?
    >
    Maybe the datetime and calendar modules may help too, but a simple solutionis
    to get your dates in number of months (tm1 and tm2 being struct_time objects
    returned by time.localtime/gmtime) :

    m1 = tm1.tm_year * 12 + (tm1.tm_mon - 1)
    m2 = tm2.tm_year * 12 + (tm2.tm_mon - 1)

    then (m1 - m2) gives the difference in months

    (see the time modules docs for more infos)

    --
    Cédric Lucantis

    Comment

    • A.T.Hofkamp

      #3
      Re: Difference between two dates

      On 2008-06-24, sniipe@gmail.co m <sniipe@gmail.c omwrote:
      Hi!
      >
      I am new in Python, so I count for your help. I need to get difference
      in months between two dates. How to do it in python? I am substracting
      two dates, for example date1 - date2 and I got result in days, how to
      change it?
      Check the 'datetime' module.

      (in general, for any given problem you have a 70% chance that somebody has
      written a module for it. Check the standard library, or else PyPI.)

      Sincerely,
      Albert

      Comment

      • sniipe@gmail.com

        #4
        Re: Difference between two dates

        Thank you for answers.

        I used Cédric Lucantis's way to resolve this problem and it works :D

        Comment

        • sniipe@gmail.com

          #5
          Re: Difference between two dates

          Thank you for answers :)

          I used Cédric Lucantis's way to resolve this problem and it works :D

          Comment

          Working...