Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help for date conversion to duration. duration must be in days.

lakshman nalla
Tera Contributor

Hi All,

 

I need help for one of the requirement.

we are having created and updated fields on my request table.

 

I need calculate duration of the request.

var duration= updated date - created date

 

The duration should be in days.

 

Below is the script:

 

 

 

 var query = gs.getMessage('task.number={0}^sla=47736d3287f4f95025abecee8bbb354c', [myNumber]);
            var grTaskSla = new GlideRecord('task_sla');
            grTaskSla.addEncodedQuery(query);
            grTaskSla.query();
 
            while (grTaskSla.next()) {
                var start_time = new GlideDateTime(grTaskSla.getValue('start_time'));
                var end_time = new GlideDateTime(grTaskSla.getValue('end_time'));
                var duration = GlideDateTime.subtract(start_time, end_time);
                return duration.getDisplayValue();
}
4 REPLIES 4

Alex Winkley
Tera Contributor

Check out the GlideDuration API - I think this will have the answer you're looking for.

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/c_GlideDurationScopedAPI

Bert_c1
Kilo Patron

Hi lakshman nalla,

 

Here's the function code from a Business rule named "Calculate Procurement task Duration":

 

 

(function executeRule(current, previous /*null when async*/) {
	var endDate = new GlideDateTime(current.work_end);
	var startDate = new GlideDateTime(current.work_start);
	var expectedStart = new GlideDateTime(current.expected_start);
	var createdDate = new GlideDateTime(current.sys_created_on);
	current.business_duration = new GlideDuration(GlideDateTime.subtract(startDate,endDate));
	if(gs.nil(startDate) || startDate =='')
		current.calendar_duration = new GlideDuration(GlideDateTime.subtract(expectedStart,endDate));
	else
		current.calendar_duration = new GlideDuration(GlideDateTime.subtract(startDate,endDate));

})(current, previous);

 

using the API Alex posted.

swathisarang98
Giga Sage

Hi @lakshman nalla ,

 

You can add one more line as below after subtracting the date as shown below which will give you the difference in days,

 

var days = duration.getRoundedDayPart();
return days;

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

 

@lakshman nalla you can use 

getRoundedDayPart() or getDayPart() both will work based on your requirement use the methods.

 

getRoundedDayPart() - Day value of the display value rounded.

Example

var dur = new GlideDuration('3 14:00:00');
gs.info(dur.getRoundedDayPart());

Output

4

 

 

getDayPart()Number of days in the duration.

Example

var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDayPart());

Output

 

3

 

 

If you require any further info on duration related API you can refer below article,

https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server/c_GlideDurationScopedAPI 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang