Odoo Aide

1

0
9 Answers
4
Avatar

Priyesh Solanki (pso)

--Priyesh Solanki (pso)--

1160
| 2 2 4
--Priyesh Solanki (pso)--
Priyesh Solanki (pso)
03/04/2013 11:02

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

2
Avatar

Borni DHIFI

--Borni DHIFI--
5525
| 7 9 11
Tunis, Tunisie
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

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

Skype : dhifi.borni

Borni DHIFI
03/04/2013 10:24

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')
5 Comments
saad
03/04/2013 10:35

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

Mike Telahun
03/04/2013 11:30

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.

Raminder Pal Singh
10/05/2013 11:53

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??

Dr Obx
21/12/2015 16:45

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
21/12/2015 16:49

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.

0
Avatar

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--

1397
| 6 5 8
Baroda, Inde
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
09/05/2018 13:31

Hello Saad,

Try to use below link.

https://stackoverflow.com/questions/4039879/best-way-to-find-the-months-between-two-dates

Hope it help you.

Best Thanks,

Ankit H Gandhi.

Poser une question
Writer
1018
saad
Étiquettes
Keep Informed
1 Abonné(e)s
About This Community

This platform 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.

Register
Odoo Training Center

Access to our eLearning platform and experience all Odoo Apps through learning videos, use cases and quizzes.

Test it now