How to calculate duration of days from current date and an end date field on form ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 02:06 AM
Hi All,
I have a section on a form with a field "Certified on" (Date type). Also, a field "Duration of days" (String type) in which I require the number of days from current date and Certified on date.
Also, The duration may get updated on certified on date change and daily based on current date.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 02:15 AM
Hello @Megha8
Plz refer this below script for your reference :-
var start = new GlideDateTime("2016-08-28 09:00:00");
var end = new GlideDateTime("2016-08-31 08:10:00");
// Duration in days
var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object
gs.print(dur.getDisplayValue()); // *** Script: 2 Days 23 Hours 10 Minutes
gs.print(dur.getDayPart()); // *** Script: 2
Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.
Regards,
Samaksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 02:33 AM
So basically you want to get difference between Certified date and Current date and store the days in that string field?
you can use subtract method and handle this in business rule
what did you start with and where are you stuck?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 02:41 PM - edited 01-25-2024 02:42 PM
I have a similar requirement, I am calculating a GlideDuration and when the end date is NULL then I am to use the present date. So if the start date is 1/24/2024 and I open the record on 1/25/2024 then I can use an on Display BR to populate the duration as 1 Day, open the record tomorrow and it will show 2 Days. In order to get that to work for a list view or report is there an option other than an on Query business rule?
Thanks,
Neal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 11:06 PM
Hi @Megha8
The dateDiff method would be what you're looking for.
dateDiff(String startDate, String endDate, Boolean numericValue)
This method will return the difference between the two dates in the format ddd hh:mm:ss, which can be used to set value for a duration field type.
So let's try to create a scheduled job that runs on a daily basic. Then query to the table to get all records having the certified date after today, and calculate the duration using dateDiff.
Sample below
var gr = new GlideRecord('<table_name>'); //your table name
gr.addNotNullQuery('certified_on'); //your certified date field name
gr.addQuery('certified_on', '>=', new GlideDateTime()); //After today
gr.query();
while(gr.next()){
var gdtToday = new GlideDateTime();
var gdtCertified = new GlideDateTime(gr.getDisplayValue());
var dur = gs.dateDiff(gdtToday.getDisplayValue(), gdtCertified);
gr.setValue('duration_of_day', dur); //your duration field name
gr.update();
}
Cheers,
Tai Vu