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.
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
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
|Asked: 2/12/15, 5:49 AM|
|Seen: 1907 times|
|Last updated: 3/16/15, 8:10 AM|