Issues with GlideDateTime()

Akiladevi Raje1
Giga Guru

when i try to execute the below code , i am getting some different date as currentdate and time , Not sure what is the issue.

 var valDays = '40';
 var gdt = new GlideDateTime();
 gdt.addDays(valDays);
 var endDate = gdt;


 gs.info("AKila testing Current valid date:valDays>" + valDays);
 gs.info("AKila testing Current valid date:gdt current dateTime>" + gdt);
 gs.info("AKila testing Current valid date:gdt add days" + gdt);
 gs.info("AKila testing Current valid date:current.variables.end_date" + endDate);
 
Response:
[0:00:00.040] Script completed in scope global: script
Script execution history and recovery available here
*** Script: AKila testing Current valid date:valDays>40
*** Script: AKila testing Current valid date:gdt current dateTime>2025-06-14 06:05:26
*** Script: AKila testing Current valid date:gdt add days2025-06-14 06:05:26
*** Script: AKila testing Current valid date:current.variables.end_date2025-06-14 06:05:26
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Akiladevi Raje1 

when you print gdt -> it already has 40 days added to it so it's printing the later date

to print the now time, you need to use separate GlideDateTime object

try this ->

 var valDays = '40';
 var nowTime = new GlideDateTime();
 var gdt = new GlideDateTime();
 gdt.addDays(valDays);

 gs.info("AKila testing Current valid date:gdt current dateTime>" + nowTime); // 
 gs.info("AKila testing Current valid date:current.variables.end_date" + gdt);
 

Output:

 

AnkurBawiskar_0-1746425668105.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Chaitanya ILCR
Kilo Patron

Hi @Akiladevi Raje1 ,

That's because you are adding 40days to using addDays() method

var valDays = 40;
var gdt = new GlideDateTime();
var curDate = new GlideDateTime()
gdt.addDays(valDays);
var endDate = gdt;

gs.info("AKila testing Current valid date:gdt current dateTime" + curDate)

gs.info("AKila testing Current valid date:valDays>" + valDays);
gs.info("AKila testing Current valid date:gdt current dateTime>" + gdt);
gs.info("AKila testing Current valid date:gdt add days" + gdt);
gs.info("AKila testing Current valid date:current.variables.end_date" + endDate);

 

try this

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Akiladevi Raje1 

when you print gdt -> it already has 40 days added to it so it's printing the later date

to print the now time, you need to use separate GlideDateTime object

try this ->

 var valDays = '40';
 var nowTime = new GlideDateTime();
 var gdt = new GlideDateTime();
 gdt.addDays(valDays);

 gs.info("AKila testing Current valid date:gdt current dateTime>" + nowTime); // 
 gs.info("AKila testing Current valid date:current.variables.end_date" + gdt);
 

Output:

 

AnkurBawiskar_0-1746425668105.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

AdusumalliK
Tera Contributor

While most of your script is right, you need to fetch display value of the added time field to get correct value. 

 

 var valDays = '40';
 var user = new GlideRecord('sys_user');
 user.get('6816f79cc0a8016401c5a33be04be441');
var timezone = user.time_zone;
 gs.info(timezone);
 var gdt = new GlideDateTime();
gdt.addDaysUTC(40);

var gdt1 = gdt.getDisplayValue();


 gs.info("AKila testing Current valid date:valDays>" + valDays);
 gs.info("AKila testing Current valid date:gdt current dateTime>" + gdt);
 gs.info("AKila testing Current valid date:gdt add days" + gdt);
 gs.info("AKila testing Current valid date:current.variables.end_date" + gdt1);
 
Mark as helpful if this helps.

Christian Webe1
Tera Contributor

I think your issue is basically that you built this up as you went and didn't insert lines at the right spots.

 

 var valDays = '40';
 var gdt = new GlideDateTime(); // Sets gdt to the current time
 gdt.addDays(valDays); // modifies gdt to be 40 days ahead of the current time
 var endDate = gdt; // sets endDate equal to gdt. Both are now 40 days ahead of the current time.


 gs.info("AKila testing Current valid date:valDays>" + valDays); 
 gs.info("AKila testing Current valid date:gdt current dateTime>" + gdt); // When looking above, this is 40 days ahead of the current time
 gs.info("AKila testing Current valid date:gdt add days" + gdt); // We are outputting the same value as above, so still 40 days ahead
 gs.info("AKila testing Current valid date:current.variables.end_date" + endDate); // As we checked in line 4, this is equal to gdt, so also 40 days ahead.

The fundamental principle we are looking at is that things are called by reference, I would suggest reading up on that in the context of JavaScript. But the TL;DR is: Variables are a reference to the actual value, so when using '=' to assign a new variable the value of an old one, you are actually assigning the reference to the value to that new variable. Both are referring to the same value though. This means when changing the new variable, it also changes the first one.

This paradigm isn't always true, for atomic variables like numbers and strings this is not the case.

 

I am aware that this explanation isn't totally correct, but it reflects enough for this specific case. I do recommend on reading up on it though! 

 

 

So to change you code so it displays what you need we could do the following:

 var valDays = '40';
 // Create a variable with the current Datetime
 var currentDateTime = new GlideDateTime();
 // Create a variable 40 days ahead of the current date time
 var endDateTime = new GlideDateTime();
 endDateTime.addDays(valDays);


 gs.info("AKila testing Current valid date:valDays>" + valDays);
 gs.info("AKila testing Current valid date:gdt current dateTime>" + currentDateTime);
 gs.info("AKila testing Current valid date:gdt add days" + endDateTime);
 gs.info("AKila testing Current valid date:current.variables.end_date" + endDateTime);

Now in further development you could use the variables 'currentDateTime' for the current timestamp, as well as 'endDateTime' for one that is 40 days ahead of the current timestamp.

 

Mark as helpful if this helped you