Odoo Help

Welcome!

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.

0

how to calculate hours and minutes between two date time fields

By
keshav
on 2/12/15, 5:49 AM 2,222 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

keshav
on 2/13/15, 12:43 AM
1

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

 

1

aneesh ATEES Infomedia Pvt Ltd

--aneesh ATEES Infomedia Pvt Ltd--
765
| 5 1 6
Thrissur, India
--aneesh ATEES Infomedia Pvt Ltd--

9995074692

aneesh ATEES Infomedia Pvt Ltd
On 2/12/15, 6:29 AM

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

0
Lavanya
On 10/26/16, 3:56 AM

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')

0
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)

Stats

Asked: 2/12/15, 5:49 AM
Seen: 2222 times
Last updated: 10/26/16, 3:56 AM