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

Anil Lande
Kilo Patron

Please check below link for similar solution.

https://www.servicenow.com/community/developer-forum/set-value-of-duration-field-via-business-rule/t...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

H S B
Giga Guru

Can you please help me understand what is the field data-type of 'Work_start'/'Work_end' & 'work_duration'? I mean is it 'Date/Time' for first 2 & 'duration' data-type for last one ?

Venkat141
Tera Contributor

Hello @H S B ,

 

Please pind below snippet.

Venkat141_0-1701273495578.png

 

My understanding is, once you find the difference between 2 date/time fields, you can directly use the output variable to set the duration. You don't have to use dur.getDisplayValue(), instead just use 'dur' to set the value.

Find below one example which I tried on my PDI -

 

1. Setting Up Duration based on two dates diff.png

 

 

fields added on Incident record.png

So following code in your scenario should work assuming you have written your code in BEFORE business-rule -

(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;
})(current, previous);

 

or if AFTER business rule being used -

 

(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;
current.setWorkflow(false);
current.autoSysFields(false);
current.update();
})(current, previous);

 

Please do mark my response as CORRECT if it solves your issue or HELPFUL as it will help others & will motivate me as well.