- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 10:06 AM
I have a Flow Designer action:
My problem is that for some reason, the format of the two input variables (start_date and end_date) are not the same format. I need someway to convert the dates into a consistent format so that the duration calculation will work properly.
Here is the input and output of the action:
the two dates: 12-7-2020 11:33:55 and 12-902020 17:44:53 are 2 days, 6 hrs, 10 min, 58 secs, NOT 11,324 days, 22 hrs, 10 min, 58 secs.
I should probably has logic in my action to detect which format each input variable is in, but I don't know how to do that.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2020 03:12 PM
I could only test the script in Flow Designer. dateDiff doesn't work in FlowDesigner. I replaced it with Subtract.
Here is the final/working code:
(function execute(inputs, outputs) {
var starting_date = new GlideDateTime(inputs.start_date);
var ending_date = new GlideDateTime(inputs.end_date);
var time_zone_adj = new GlideTime();
time_zone_adj.setValue("08:00:00");
starting_date.add(time_zone_adj);
ending_date.add(time_zone_adj);
var schedule = new GlideSchedule(inputs.schedule_sys_id);
var duration = schedule.duration(starting_date, ending_date);
//outputs.message = "Schedule Duration: " + duration.getDurationValue();
outputs.starting_date = starting_date;
outputs.ending_date = ending_date;
outputs.duration = duration;
})(inputs, outputs);
One thing I figured out is that my dates were Local Timezone and GlideDateTime assumes they are UTC. So I had to add 8 hours to the start and end dates.
I also dropped the getDurationValue from the assignment of outputs.duration.
ServiceNow is good at converting dates of various formats to a standard time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 10:14 AM
I believe you should be knowing the format so that you can set it correctly
refer sample links below for help in the scripting
how to convert date /time to ist timezone and also change format?
convert date to DD/MM/YYYY format servicenow portal
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 10:41 AM
Hi,
Kindly use getByFormat() function to get in your required format. Here is the sample code that you can use in your script step to get in the same format as start date
var gdt = new GlideDateTime(end_date); //put your variable name
var dt = gdt.getDate().getByFormat("M-dd-YYYY") +" "+ gdt.getTime().getByFormat("HH:mm:ss");
end_date=new GlideDateTime(dt);
Mark the comment as a correct answer and also helpful if this has answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 11:44 AM
It turns out my problem is NOT a date format issue. When I added the start date and end date as output parameters, they both came out in the same format. (mm-dd-yyyy hh:mm:ss).
HOWEVER, I am still getting a duration of 11,354 days, 6 hrs, 10 min, 58 secs. I have tried a variety of standard schedules and my custom schedule works in a Client Script, but this is in a flow designer action.
Why is the duration so far off?
again, here is my action:
(function execute(inputs, outputs) {
var schedule = new GlideSchedule();
schedule.load(inputs.schedule_sys_id);
var starting_date = new GlideDateTime(inputs.start_date);
var ending_date = new GlideDateTime(inputs.end_date);
outputs.duration = (schedule.duration(starting_date, ending_date).getDurationValue());
outputs.starting_date = starting_date;
outputs.ending_date = ending_date;
})(inputs, outputs);
and here are my results:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 12:21 PM
Try to do liek this
var dur = schedule.duration(startDate, endDate);
outputs.duration=dur.getDurationValue();