Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to calculate time between two fields Date type using JavaScript?

    Thanks so much! I need to pay more attention XD

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to calculate time between two fields Date type using JavaScript?

    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
    Community Member Microsoft Employee on at
    RE: How to calculate time between two fields Date type using JavaScript?

    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?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans