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)

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

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