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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to calculate time between two fields Date type using JavaScript?

(0) ShareShare
ReportReport
Posted on by

I have two fields Date type, i would like about how to calculate time among them 2 using JavaScript

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    In addition to what Guido states, you'll have to take some other things into account...

    • What unit is your math in?
      • For the best accuracy, do the math in milliseconds as Guid's stackoverflow link suggests, and then convert to your desired unit.
    • With the two date fields in question, will they ever span a Daylight Saving Time change?
  • Community Member Profile Picture
    on at

    My code

    var dt1= Xrm.Page.getAttribute("new_date1").getValue();

    var dt2= Xrm.Page.getAttribute("new_date2").getValue();

    var h= Xrm.Page.getAttribute("new_hours").getValue();

    var hours = 60*60*1000;

    if (date1!= null && date2!=null) {

    var diffHours = Math.round(Math.abs((dt2.getTime() - dt1.getTime())/(hours)));

    }

    h.setValue(diffHours );

    ------

    but don't working

  • Community Member Profile Picture
    on at

    Thanks so much! I need to pay more attention XD

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans