Difference between two dates

S

sniipe

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
 
C

Cédric Lucantis

Le Tuesday 24 June 2008 12:11:03 (e-mail address removed), 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 solution is
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)
 
S

sniipe

Thank you for answers.

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

sniipe

Thank you for answers :)

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

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top