- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:05 PM
when i try to execute the below code , i am getting some different date as currentdate and time , Not sure what is the issue.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:16 PM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:16 PM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:43 PM
While most of your script is right, you need to fetch display value of the added time field to get correct value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 02:30 AM
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