- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 04:43 AM
The GlideDateTime's addDays not working as expected. This function is not returning correct value for time
In the below example I am taking time of 90 days back from current date. However the function is returning correct value till '-78' days but not working as expected for values greater than 78.
I am wondering if this is bug in service now or am I missing any step here.
Sample Code
var toDateTime = new GlideDateTime(gs.now());
var gtime = new GlideTime();
gtime.setValue("23:59:59");
toDateTime.add(gtime);
var fromDateTime = new GlideDateTime(toDateTime.now());
fromDateTime.addDays(-90); //fromDateTime.addDays(-78); works fine till 78
gs.log("From Date Time " + fromDateTime);
gs.log("To Date Time " + toDateTime);
Output:
*** Script: From Date Time 2019-02-27 00:59:59
*** Script: To Date Time 2019-05-27 23:59:59
*** Script: Expected From Date Time 2019-02-27 23:59:59
Instead of returning 23:59:59 function is returning 00:59:59
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 10:12 PM
I'd recommend you to use addDaysUTC instead of addDays:
var toDateTime = new GlideDateTime(gs.now());
var gtime = new GlideTime();
gtime.setValue("23:59:59");
toDateTime.add(gtime);
var fromDateTime = new GlideDateTime(gs.now());
fromDateTime.addDaysUTC(-90); //fromDateTime.addDaysUTC(-78); works fine till 78
gs.log("From Date Time " + fromDateTime);
gs.log("To Date Time " + toDateTime);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 05:40 AM
Hi Sameer,
I don't think there is any class with name GlideTime, Instead try using GlideDate and see If that works.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 07:41 AM
@Narenda, there is a GlideTime class.
@Sameer - the reason for the time change from 23:59:59 to 00:59:59 is because there was a time change (in most of the world) between February and May. Most places in the northern hemisphere shifted their clocks ahead one hour in the spring, hence the time difference.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 10:12 PM
I'd recommend you to use addDaysUTC instead of addDays:
var toDateTime = new GlideDateTime(gs.now());
var gtime = new GlideTime();
gtime.setValue("23:59:59");
toDateTime.add(gtime);
var fromDateTime = new GlideDateTime(gs.now());
fromDateTime.addDaysUTC(-90); //fromDateTime.addDaysUTC(-78); works fine till 78
gs.log("From Date Time " + fromDateTime);
gs.log("To Date Time " + toDateTime);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2019 03:56 AM
@Chuck: Thanks for information, this is very helpful.
olegki's solution is as per my requirement.