Need help for date conversion to duration. duration must be in days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:13 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:32 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 12:43 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 01:10 PM - edited 02-19-2024 01:12 PM
@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