web
You’re offline. This is a read only version of the page.
close
Skip to main content
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. 

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How can I get the total duration of 2 fields?

    Hello,

    What types of fields new_starttime/new_endtime?

  • Verified answer
    prt33k Profile Picture
    6,907 on at
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    Hi Sardar,

    I tried it but did not work.

    pastedimage1612268723338v1.png

    pastedimage1612268762897v2.png

    Thank you

  • lynp Profile Picture
    5 on at
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    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
    RE: How can I get the total duration of 2 fields?

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 60

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 26 Super User 2025 Season 2

#3
CU29080825-0 Profile Picture

CU29080825-0 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans