# Datetime calculation

• ### Question

• Hi,

Is it possible to calculate the duration between 2 datetime fields?
For example. (Start time) - (End Time).
The result should be in hours for further calculations.

regards...
Wednesday, February 4, 2009 10:29 AM

•  var date1 = Date.parse(crmForm.all.new_date1.DataValue); var date2 = Date.parse(crmForm.all.new_date2.DataValue); alert((date2-date1)/(60*60*1000));

Cheers,
Jim
Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com , http://mscrm.cn
Wednesday, February 4, 2009 11:36 AM
• If you want to calculate this within SQL query function then you can use standard SQL  DATDIFF() function
Syntax:
DATEDIFF (hour, <date1>, <date2>)
e.g.
Select DATEDIFF (hour, GETDATE () + 1, GETDATE ())
Result:
-24

• Marked as answer by Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:47 AM

### All replies

•  var date1 = Date.parse(crmForm.all.new_date1.DataValue); var date2 = Date.parse(crmForm.all.new_date2.DataValue); alert((date2-date1)/(60*60*1000));

Cheers,
Jim
Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com , http://mscrm.cn
Wednesday, February 4, 2009 11:36 AM
• If you want to calculate this within SQL query function then you can use standard SQL  DATDIFF() function
Syntax:
DATEDIFF (hour, <date1>, <date2>)
e.g.
Select DATEDIFF (hour, GETDATE () + 1, GETDATE ())
Result:
-24

• Marked as answer by Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:47 AM
• Thx Jim, this really helps me.
but how can i store the result into a decimal or integer field. Do i have to cast the result?
ToDecimal() does not work.

My code is like this:

startdate field:
 new_actualhours_onchange0();

enddate field:
 new_actualhours_onchange0();

actualhours field: