Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

Posted on by Microsoft Employee

Hey I need to know how can i do the validity duration and set it automatically as the alculated duration between Valid From and Valid To (in hours and in days) using JavaScript..

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

    Mark the answer verified if it answers your question

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

    Thank you Saad

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

    If you want to set duration days and hours in same field then this filed should be Single Line of text. Use this script:

    function DateDiff() {

    var start = Xrm.Page.getAttribute('validfrom').getValue();

    var end = Xrm.Page.getAttribute('validto').getValue();

    // to cancel the error when undefined

    if (start == undefined || start == null || end == undefined || end == null) {

    return;

    }

    // Get 1 hour

    var hour = 1000 * 60 * 60;

    // Get time in hours

    var hours = ((end - start) / hour);

    // get Time in days

    var daysToSet = Math.floor(hours/24);

    // Get hours to set

    var hoursToSet = hours % 24;

    // Can't be less than 0.

    if (daysToSet < 0)

    daysToSet = 0;

    if (hoursToSet < 0)

    hoursToSet = 0;

    var duration = daysToSet.toString() + " " + hoursToSet.toString();

    // Set the duration

    Xrm.Page.getAttribute('duration').setValue(duration);

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

    I have in my form 2 fields : Valid to & Valid from, I need to calculate the Validity duration using java script; For example the time consumed doing a task

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a field “Validity Duration” on the form. and set it automatically as the calculated duration between Valid From and Valid To (in hours and in days)

    Please elborate what you want to achieve with an example. Unable to understand validity duration

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