Notifications
Announcements
No record found.
Hi,
I want to calculate the duration in hours between two date and time fields using JavaScript.
*This post is locked for comments
Hi
Just subtract the earlier datetime value from the later datetime value. This would give you the value in millisecond. Get the hours from this.
Example (you refer the following code, obtain the values from the 'Date and Time' fields in the form using Xrm.Page.getAttribute and subtract the earlier date from the later date):
var startDateTime = new Date(2007, 8, 25, 22, 25, 26, 456); var endDateTime = new Date(2017, 8, 25, 22, 25, 26, 457); var duration = (endDateTime - startDateTime)/36e5;//returns the difference in hours
Hi Dinoop,
1. You should get the Datetime field by CRM SDKvar dateTime1 = Xrm.Page.getAttribute("datetime1Field_name").getValue();var dateTime2 = Xrm.Page.getAttribute("datetime2Field_name").getValue();
2. calculate the difference
dateTimeDiff = dateTime1 - dateTime2;var diffhours = (dateTimeDiff /1000) / 3600;// alert(diffhours);
Please mark correct if it help you :)
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2