Skip to content Skip to sidebar Skip to footer

Convert Char To Datetime Odoo 9

I have two char fields, data import from excel or csv in odoo. time_1= fields.Char(string = 'Time 1') time_2= fields.Char(string = 'Time 2') result= fields.Float(string = 'Result

Solution 1:

It should be like that,

from datetime import datetime

@api.multi
@api.onchange('time_1', 'time_2') 
def onchange_time(self):
    for rec in self:
        time1 = datetime.strptime(rec.time1, "%Y-%m-%d %H:%M:%S")
        time2 = datetime.strptime(rec.time2, "%Y-%m-%d %H:%M:%S")
        rec.result = (time2 - time1).seconds / float(60*60)

Two datetime objects will return timedelta obect in result while you perform any arithmetic operations on it. timedelta has property while will give you difference in seconds, so you can convert that seconds to hours.

And then in view set

<fieldname="result"widget="float_time" />

Post a Comment for "Convert Char To Datetime Odoo 9"