472,352 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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
Jun 27 '08 #1
4 6559
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
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
Thank you for answers.

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the...
6
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone...
4
by: Yotam | last post by:
Hi, I need some help with JS. I will be grateful, if you can help me out. I have two date fields (check in, check out) and "number of days"...
2
by: Blackmore | last post by:
I am trying to use javascript to calculate the difference between two form inputted dates and return the result to another form object. When I load...
8
by: Claudia Fong | last post by:
Hi, In VB we have DateDiff to calculate the days difference between 2 dates, I was wondering if we have something like that in C#? I want...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi, misters which the best method for get the difference (in days) of two dates. The hour (hh:mm) don't mind, would be 00:00 for all dates. ...
3
by: prashantkuppa | last post by:
hi, iam struggling with difference between two dates of format type dd/mm/yyyy i need difference between two dates olddate(dd/mm/yyyy) -...
4
by: lenygold via DBMonster.com | last post by:
I found this example in MYSQL: create table events ( id integer not null primary key , datetime_start datetime not null , datetime_end ...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.