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

Mohan raj
Mega Sage

Hi @Venkat141,

 

To get the difference between the two date and populate the duration in the field which has duration field type. Try to use below script it worked for me.

 

(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 diff = gs.dateDiff(gdt1, gdt2, true);
var d = Math.floor(diff / (3600 * 24));
var h = Math.floor(diff % (3600 * 24) / 3600);
var m = Math.floor(diff % 3600 / 60);
var s = Math.floor(diff % 60);
var time = d + ' ' + h + ':' + m + ':' + s;
var dur = new GlideDuration(time)

current.work_duration = dur;

})(current, previous);

 

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.

Hello @Mohan raj ,

 

I tried above code and it's not updating. Could you please check the Info message.

 

Venkat141_1-1701274816735.png

 

Hi @Venkat141,

 

try to use update operation, 

current.update() in Business rule.

 

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.

Hello, 

 

Now it's working. Could you please explain the code

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.