Answered by:
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
Answers
-
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- Proposed as answer by Jim Wang (Microsoft)Microsoft employee, Moderator Wednesday, February 4, 2009 11:37 AM
- Marked as answer by cam2huynh Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:36 AMModerator -
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 cam2huynh Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:47 AMAnswerer
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- Proposed as answer by Jim Wang (Microsoft)Microsoft employee, Moderator Wednesday, February 4, 2009 11:37 AM
- Marked as answer by cam2huynh Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:36 AMModerator -
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 cam2huynh Wednesday, February 4, 2009 12:51 PM
Wednesday, February 4, 2009 11:47 AMAnswerer -
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:alert((crmForm.all.new_end.DataValue-crmForm.all.new_start.DataValue)/(60*60*1000));
This works perfectly fine with the alert popup window. But how can i store/show this in the actualhours field. At the moment its a decimal field.
thx
- Edited by cam2huynh Wednesday, February 4, 2009 2:19 PM
Wednesday, February 4, 2009 12:50 PM -
Never mind i've already solved it.
My bad.
Thank u very much!
Wednesday, February 4, 2009 12:59 PM