This question has been flagged
5 Replies
25973 Views

I have two date time fields

'`date_from`': fields.datetime('Submission Date').

'`date_to`': fields.datetime('Return Date').

and I want to capture difference between two date time fields in "H":"m" format

in  'total_hours': fields.function(_calculate_total_hours,string="Total Hours", type="float"),

in submision stage I need to use only submission date that means total_hours are zero

in second stage will add return date and capture the total_hours and minutes.

how can I do this please help me.

 

 

Avatar
Discard
Author

Its working but getting only hours I need minutes also

Best Answer

cheak this site :https://docs.python.org/2/library/datetime.html#module-datetime

Avatar
Discard
Best Answer

Hi,

For do what you want you need to make the diff_hrs at a functionnal fields how call a function,

this function return what you want with some code

for help : https://docs.python.org/2/library/datetime.html

 

Avatar
Discard
Best Answer

date1 = '2016-10-26 11:53:08.018805'

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

 date2 = '2016-10-27 10:23:08.018805'

date11 = datetime.datetime.strptime(date1, datetimeFormat)

date12 = datetime.datetime.strptime(date2,datetimeFormat)

timedelta = date11 - date12
hour1 = timedelta.days * 24

hours = (timedelta.seconds) / 3600

0overall_hour = hour1 + hoursprint (overall_hour , ' Hours')

Avatar
Discard
Best Answer

Following snippet should help you.

def _calculate_total_hours(self, cr, uid, ids, field_name, arg, context):
    for obj in self.pool.get('xxxxxx').browse(cr,uid,ids,context=context)
       date_from = datetime.strptime(obj.date_from, DEFAULT_SERVER_DATE_FORMAT) # change the format of the date if require
       date_to = datetime.strptime(obj.date_to, DEFAULT_SERVER_DATE_FORMAT) # change the format of the date if required
       difference = 
date_to - date_from
       res[obj.id] = difference.seconds / 3600 # float in hours
    return res

 


 

 

Avatar
Discard