Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
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.
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')
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
This platform 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.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 2/12/15, 5:49 AM|
|Seen: 2471 times|
|Last updated: 10/26/16, 3:56 AM|