GlideDateTime's addDays not working as expected

Sameer18
Tera Contributor

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

 

1 ACCEPTED SOLUTION

Oleg
Mega Sage

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);

View solution in original post

5 REPLIES 5

Narendra Kota
Mega Sage

Hi Sameer,

 

I don't think there is any class with name GlideTime, Instead try using GlideDate and see If that works.

Chuck Tomasi
Tera Patron

@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.

Oleg
Mega Sage

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);

Sameer18
Tera Contributor

@Chuck: Thanks for information, this is very helpful.

olegki's solution is as per my requirement.