Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How do I calculate number of months between two dates ?

By
saad
on 4/3/13, 9:53 AM 12,621 views

I have the need to find the number of months between two dates in python. Is there any standard python method to do that ?

Can yo be more specific? How many months should be between april 30 and may 1st of the same year? And what about 1-st and last day of a specific month. And what about 15 of month x and 14 of month x+1?

patrick
on 4/3/13, 10:39 AM
4

Priyesh Solanki (pso)

--Priyesh Solanki (pso)--
Tiny ERP Pvt Ltd
http://www.odoo.com
1100
| 2 2 4
--Priyesh Solanki (pso)--
Priyesh Solanki (pso)
On 4/3/13, 11:02 AM

You can try this:

from datetime import datetime
from dateutil import relativedelta
date1 = datetime.strptime(str('2011-08-15 12:00:00'), '%Y-%m-%d %H:%M:%S')
date2 = datetime.strptime(str('2012-02-15'), '%Y-%m-%d')
r = relativedelta.relativedelta(date2, date1)
r.months

This will give you number of months between two dates.

Thanks, Priyesh Solanki

1

Borni DHIFI

--Borni DHIFI--
5102
| 6 8 9
Tunis, Tunisia
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

http://www.linkedin.com/in/borni

Skype : dhifi.borni

Borni DHIFI
On 4/3/13, 10:24 AM

for example:

from datetime import datetime
def days_between(d1, d2):
    d1 = datetime.strptime(d1, "%Y-%m-%d")
    d2 = datetime.strptime(d2, "%Y-%m-%d") 
    return abs((d2 - d1).days)

print days_between('2013-05-06', '2013-06-06')

Thank you for your answer, but I need to calculate number of month. The division by 30 doesn't give an exact result.

saad
on 4/3/13, 10:35 AM

If you want the number of months you should use dateutil.relativedelta, as in Priyesh's answer. The reason for that is because it hides all the complex calculations required for different lengths of months, leap years, etc.

Mike Telahun
on 4/3/13, 11:30 AM

If I have inputs fields like date_from and date_to in pay slip calculation and i have to find the number of days between them . How can i use that ?? Can i use some variable of that kind in python code.?? Above code will work there??

Raminder Pal Singh
on 5/10/13, 11:53 AM

I would like to refresh a bit this subject. Guys, how ... i tried Priyesh's answer but because I'm trying to provide date's from inputs ... it gives me an error: date1 = datetime.strptime(str('st_date'), '%Y-%m-%d %H:%M:%S') File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime (data_string, format)) ValueError: time data 'st_date' does not match format '%Y-%m-%d %H:%M:%S' What to do with it ?

Dr Obx
on 12/21/15, 4:45 PM

or this: date1 = datetime.strptime(str(st_date), '%Y-%m-%d %H:%M:%S') NameError: name 'st_date' is not defined 2015-12-21 21:48:00,534 3121 WARNING mydatabase openerp.sql_db: Cursor not closed explicitly Please enable sql debugging to trace the caller.

Dr Obx
on 12/21/15, 4:49 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/3/13, 9:53 AM
Seen: 12621 times
Last updated: 3/16/15, 8:10 AM