Skip to main content

Notifications

Announcements

No record found.

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. 

  • prt33k Profile Picture
    prt33k 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

  • lynp Profile Picture
    lynp 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
    prt33k 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
    lynp 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

  • lynp Profile Picture
    lynp 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
    lynp 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

  • Suggested answer
    Sardar Haseeb Profile Picture
    Sardar Haseeb 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);

  • Verified answer
    prt33k Profile Picture
    prt33k 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);
    

  • a33ik Profile Picture
    a33ik 84,325 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?

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!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans