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 :
Service | Customer Service, Contact Center, Fie...
Answered

How can I get the total duration of 2 fields?

(0) ShareShare
ReportReport
Posted on by 5

How can I get the total duration of this 2 field?

pastedimage1612094784336v4.png

Start and End time are both single line of text. 

This is my current code:

function GetTotalDuration(executionContext) {

var formContext = executionContext.getFormContext();

var varStartTime = formContext.getAttribute("new_starttime").getValue();
var varEndTime = formContext.getAttribute("new_endtime").getValue();

var varDuration = varEndTime - varStartTime

alert(varDuration);

}

the alert result is 7247:16. This should be 1 hour and 8 mins. 

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    What types of fields new_starttime/new_endtime?

  • Verified answer
    prt33k Profile Picture
    6,907 on at

    try this:

    var formContext = executionContext.getFormContext();
    var varStartTime = formContext.getAttribute("new_starttime").getValue();
    var varEndTime = formContext.getAttribute("new_endtime").getValue();
    var startTime= new Date();
    startTime.setHours(varStartTime.substr(0,varStartTime.indexOf(":")));
    startTime.setMinutes(varStartTime.substr(varStartTime.indexOf(":") 1));
    startTime.setSeconds(0);
    
    var endTime= new Date();
    endTime.setHours(varEndTime.substr(0,varEndTime.indexOf(":")));
    endTime.setMinutes(varEndTime.substr(varEndTime.indexOf(":") 1));
    endTime.setSeconds(0);
    
    var varDuration = endTime- startTime;
    var hh = Math.floor(varDuration / 1000 / 60 / 60);
    varDuration -= hh * 1000 * 60 * 60;
    var mm = Math.floor(varDuration / 1000 / 60);
    alert(hh  ":" mm);
    

  • Suggested answer
    Sardar Haseeb Profile Picture
    25 on at

    Try this:

              var varStartTime = new DateTime(context.getAttribute("new_starttime").getValue());

              var varEndTime = new DateTime(context.getAttribute("new_starttime").getValue());

                var varDuration=EndTime-StartTime;

    alert(varDuration);

  • lynp Profile Picture
    5 on at

    currently they're single line of text - string. I want them to be decimal so I can calculate them

  • lynp Profile Picture
    5 on at

    Hi Sardar,

    I tried it but did not work.

    pastedimage1612268723338v1.png

    pastedimage1612268762897v2.png

    Thank you

  • lynp Profile Picture
    5 on at

    Hi Prateek, it is not working if I'm putting it on the whole number or decimal data type. but if single line of text/string it will work. I need the data type field to be whole number or decimal since I will compute all the duration later on.

    Thank you

  • prt33k Profile Picture
    6,907 on at

    Hi,

    If it is going to be decimal then what format are you going to store because you cant have semicolon in a decimal - so having "h:m" format wont work?

    Thanks,

    Prateek

  • lynp Profile Picture
    5 on at

    Hi Prateek,

    Currently this is my sample record. Start time and end time is single line of text or string. 

    pastedimage1612280942833v1.png

    I need to put the duration of start time and end time to decimal which should be 2.00.

    Thank you

  • prt33k Profile Picture
    6,907 on at

    I that case you can simply use toString() to convert the decimal to string and then use '.' instead of ':' in finding indexof

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 60 Most Valuable Professional

#2
11manish Profile Picture

11manish 23 Super User 2026 Season 2

#3
Travis South Profile Picture

Travis South 10

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans