I cannot able to set the duration between two date fields

Venkat141
Tera Contributor

Hello All,

 

@Ankur Bawiskar  

@Saurav 

@shloke04 

@Anil Lande 

I cannot able to set the duration between two date fields.

 

Below is the code which I written.

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    var startDate = current.work_start;
    var endDate = current.work_end;
var gdt1 = new GlideDateTime(startDate);
var gdt2 = new GlideDateTime(endDate);
var dur = GlideDateTime.subtract(gdt1, gdt2);
current.work_duration = dur.getDisplayValue();
//gs.addInfoMessage(dur.getDisplayValue());
})(current, previous);
1 ACCEPTED SOLUTION

Hi @Venkat141,

 

I have add the comment to understand the code.

var gdt1 = new GlideDateTime("28-11-2023 00:00:00");
var gdt2 = new GlideDateTime("21-11-2023 00:00:00");
var diff = gs.dateDiff(gdt1, gdt2, true); // getting difference in milli seconds
var d = Math.floor(diff / (3600 * 24)); // calculate the number of days 
var h = Math.floor(diff % (3600 * 24) / 3600); //calculate the number of hours
var m = Math.floor(diff % 3600 / 60); // calculate the number of Mintues
var s = Math.floor(diff % 60);  // calculate the number of seconds

//Now got all details like days, hours, mintues and seconds 
// to populate the duration field need to be in below format
// 06 12:00:00 --> days hours:minutes:seconds
var time = d + ' ' + h + ':' + m + ':' + s;  

//The default date in JavaScript is January 1, 1970, 00:00:00 

var dur = new GlideDuration(time) //GlideDuration is convert this format 06 12:00:00 into Date and time format for example : 1969-12-25 00:00:00 
//By setting the date and time (1969-12-25 00:00:00) to the duration field type, it calculates the difference between the default date and the date that is entered. The value of this difference is then set in the duration field.
current.work_duration = dur; //setting the value 
current.update(); //updating the form

 

Hard coded date and time to explain in the script, you can use the field name.

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

 

View solution in original post

10 REPLIES 10

Tai Vu
Kilo Patron
Kilo Patron

Hi @Venkat141 

The Duration field type is expecting the value in the format "ddd hh:mm:ss." After your calculation, ensure that you return the duration in the correct format.

Let's use the API below, it will return the difference between the two dates in the format ddd hh:mm:ss.

dateDiff(String startDate, String endDate, Boolean numericValue)

Returns
TypeDescription
StringIf the numericValue parameter is true, returns the difference between the two dates as an integer number of seconds; if false, returns the difference between the two dates in the format ddd hh:mm:ss.

 

Ref: Calculate difference bet two dates and set duration.

 

Let's try my adjustment below.

(function executeRule(current, previous /*null when async*/ ) {

    var startDate = current.work_start;
    var endDate = current.work_end;
    var duration = gs.dateDiff(startDate, endDate);
    current.work_duration = duration;

})(current, previous);

 

 

Cheers,

Tai Vu