web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calculate duration between two date and time fields using JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi,

I want to calculate the duration in hours between two date and time fields using JavaScript.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

    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
  • Verified answer
    Edison Lai Profile Picture
    85 on at

    Hi Dinoop,

    1. You should get the Datetime field by CRM SDK
    var 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.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans