How to calculate difference between two timestamps in number of days ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 09:39 PM
Hi All,
We have a requirement to calculate the number of days between 2 datetimestamps.
e.g :-
Scenario :-
Date1 :- 05-07-2017 13:00:00
Date2 :- 06-07-2017 11:00:00
Now i try to convert both the date in secs and then took the difference and then divided by number of seconds in a day. It will give me value as slightly less than 1. If i use Math.Ceil(0.9) =1
So i will end up getting number of days as 1, but in actual it should be 2, as it used both the days.
If i use Math.Ceil(0.9) + 1 = 2 , then it will give me correct result. But i can't use this as it will lead to wrong result in below situation :-
Date1 :- 05-07-2017 13:00:00
Date2 :- 06-07-2017 14:00:00
diff will come out to be slightly higher than 1, assume it to be 1.1
here , Math.Ceil(1.1) + 1 = 2+1 = 3, which is wrong in this also the actual anwer should be 2.
So could someone please help me in understanding on how can we calculate this.
Thanks,
Gurav
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 09:52 PM
How about using round() function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 11:29 PM
I would recommend to check this wiki page : http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#gsc.tab=0

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 11:34 PM
Hello Gaurav,
Refer the below links may helpful to you.
Find difference between two dates
http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#gsc.tab=0 \\
ServiceNow Commnunity MVP -2018 class.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 12:41 AM
Hi Gaurav,
Are you looking this kind of a result and solution.
var gdt1 = new GlideDateTime(current.arrival.getDisplayValue());
var gdt2 = new GlideDateTime(current.departure.getDisplayValue());
var dur = GlideDateTime.subtract(gdt1, gdt2);
current.duration=dur.getDisplayValue();
I wrote this in the business rules and the example result I got was as below for the input I gave in.
Start Date: 2017-07-26 00:37:24
End Date: 2017-07-27 01:37:30
Duration Difference: 1 Day 1 Hour
Please mark as helpful or Correct based on the impact.
Regards
Param