Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

date calculation between 2 date fields

Posted on by 140

Hi, 

i have a case im working on which is get a difference between 2 days, here is the snap of that.

timing.PNG

so basically this is to track a time between cases we re handling, and i got it working by Work Flow, but now my form is extremely slow when loading, so im look at option to do this on form using JS, so "Assigned on" and "closed on" will still have a Work flow on and i need to write some JS to Time on Queue( Assigned on - Closed on) and Assigned to end ( Assigned on - closed on), what would be the best way to tackle this situation ?

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: date calculation between 2 date fields

    Hi Kalana1985,

    This topic discusses calculating the difference between two dates (excluding weekend days) using JavaScript. This information can help you to modify your date calculation function.

  • Kalana1985 Profile Picture
    Kalana1985 140 on at
    RE: date calculation between 2 date fields

    Thank you Adrian, i refactors your code and its working as expected,

    function dateCalculation() {

       debugger;

       var assignedOn = Xrm.Page.getAttribute("hqi_assignedon").getValue();

       var closedOn = Xrm.Page.getAttribute("hqi_closedon").getValue();

       var timeDifference = closedOn - assignedOn;

       var differenceInHours = (timeDifference / 1000) / 3600;

       if (timeDifference!=null) {

           Xrm.Page.getAttribute("hqi_assigntoendtime").setValue(differenceInHours);

       }

    };

    now i have to make this function little smarter by removing weekends and non working Hrs from "differenceInHours " calculation, any suggestion would greatly appreciated.

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: date calculation between 2 date fields

    You can also refer http://himbap.com/blog/?p=154

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: date calculation between 2 date fields

    Hi Kalana1985,

    Here is some sample JavaScript code based on your problem. Modify it to suit your needs.

    var assignedOn = Xrm.Page.getAttribute("assignedon").getValue();
    var closedOn = Xrm.Page.getAttribute("closedon").getValue();
    timeDifference = assignedOn - closedOn;
    var differenceInHours = (timeDifference /1000) / 3600;
    

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans