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

Difference between two dates

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Le Tuesday 24 June 2008 12:11:03 sn****@gmail.com, 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
Jun 27 '08 #2

P: n/a
On 2008-06-24, sn****@gmail.com <sn****@gmail.comwrote:
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
Jun 27 '08 #3

P: n/a
Thank you for answers.

I used Cédric Lucantis's way to resolve this problem and it works :D
Jun 27 '08 #4

P: n/a
Thank you for answers :)

I used Cédric Lucantis's way to resolve this problem and it works :D
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.