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.

0

Datetime in python

By
Demirel yakito
on 12/31/14, 7:27 AM 2,687 views

i want to find the time difference between two time in python. this is my code i got a type error. is this FMT is correct for this datetime?

import time

from datetime import datetime
s1=datetime.now()
print s1
time.sleep(2)
s2=datetime.now()
print s2

FMT = '%Y-%m-%d %H:%M:%s'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
print tdelta

 

 

output is:

2014-12-31 17:33:59.692952
2014-12-31 17:34:01.695124
Traceback (most recent call last):
  File "/opt/odoo/trial_projects/check.py", line 14, in <module>
    tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
TypeError: must be string, not datetime.datetime

 

3

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 12/31/14, 8:24 AM

Hello,

when you're trying this:

tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)

the s2 and s1 are datetime.datetime objects so you got the error.
you have to change the FMT to :

FMT = '%Y-%m-%d %H:%M:%S.%f'

because .now() return time with msconds ...

then use

tdelta = datetime.strptime(str(d2),FMT) - datetime.strptime(str(d1),FMT)

then you'll get datetime.timedelta object ....

I hope this will help you ...

 

2
kirubanidhi
On 6/9/16, 6:11 AM

Hai demirel,

     Please specify a header file,

                    from datetime import datetime

     First create a two datetime field in py file,

ex:

                    start_work = fields.Datetime(string="Start work")

                    end_work = fields.Datetime(string="End work")

                    work_hours=fields.Float(string="Total Working Hours",compute='sal_cal',store=True)

then create a compute function name as sal_cal

                    @api.depends('start_work','end_work')

                    def sal_cal(self):

                        t1= datetime.strptime(self.start_work, '%Y-%m-%d %H:%M:%S')

                        t2= datetime.strptime(self.end_work, '%Y-%m-%d %H:%M:%S')

                        s= datetime.strptime(str(t2.time()),'%H:%M:%S') - datetime.strptime(str(t1.time()),'%H:%M:%S')

In this above code is working perfectly to me.please check it now and let me know if any issue are occured


In this above code output is:

2016-06-09 02:30:16

02:30:16

2016-06-09 09:48:43

09:48:43

7:18:27

kirubanidhi
on 6/9/16, 6:13 AM
1
Anand
On 12/31/14, 7:37 AM

use this for ref.... surely it will heelp you..

from datetime import datetime
d1='2014-11-15'
d2='2013-11-15'
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
print abs((d2 - d1).days)/(12*30)
print datetime.now().date()

Hi Anand your code show's date alone not the time.

Gopalakrishnan Kasilingam
on 12/31/14, 7:43 AM

just add like abs((d2 - d1).days)*24

Anand
on 12/31/14, 7:48 AM
0
Demirel yakito
On 12/31/14, 7:44 AM

i want to find day with time difference...


 

just add like abs((d2 - d1).days)*24

Anand
on 12/31/14, 7:52 AM

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: 12/31/14, 7:27 AM
Seen: 2687 times
Last updated: 6/9/16, 6:11 AM