Odoo Help


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.


how to calculate hours and minutes between two date time fields

on 2/12/15, 5:49 AM 1,907 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.



Its working but getting only hours I need minutes also

on 2/13/15, 12:43 AM


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


On 2/12/15, 6:29 AM

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

Jordan Vrtanoski
On 2/12/15, 7:32 AM

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




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)


Asked: 2/12/15, 5:49 AM
Seen: 1907 times
Last updated: 3/16/15, 8:10 AM