- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2025 02:31 AM
Hi,
I'm not good with dates and times, so I'm practicing now. Kindly help.
var gdt = new GlideDateTime("2025-07-03 08:03:35");
var adding = gdt.addDays(10).toString(); //add 10 days to current date time
var removing = gdt.addDays(-1); //remove 1 days from current date time
gs.addInfoMessage(gdt);
gs.addInfoMessage(adding);
gs.addInfoMessage(removing);
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2025 02:38 AM
addDays() doesn't return anything.
You can always go to Docs for what that method does
That's the best way to learn and explore.
var gdt = new GlideDateTime("2025-07-03 08:03:35");
gdt.addDays(10); //add 10 days to current date time
gs.addInfoMessage('adding' + gdt.getValue());
var nowTime = new GlideDateTime();
nowTime.addDays(-1); //remove 1 days from current date time
gs.addInfoMessage('removing' + nowTime.getValue());
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
07-03-2025 02:38 AM
addDays() doesn't return anything.
You can always go to Docs for what that method does
That's the best way to learn and explore.
var gdt = new GlideDateTime("2025-07-03 08:03:35");
gdt.addDays(10); //add 10 days to current date time
gs.addInfoMessage('adding' + gdt.getValue());
var nowTime = new GlideDateTime();
nowTime.addDays(-1); //remove 1 days from current date time
gs.addInfoMessage('removing' + nowTime.getValue());
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
07-03-2025 02:41 AM
Hi @ServiceNow Use6,
addDays() modifies the GlideDateTime in place. So when you call it twice on the same gdt, the date keeps changing. Also, addDays() returns void, so adding and removing don't hold new date strings like you expect.
Use the following script:
var gdt = new GlideDateTime("2025-07-03 08:03:35");
var gdtAdd = new GlideDateTime(gdt);
gdtAdd.addDays(10);
var gdtRemove = new GlideDateTime(gdt);
gdtRemove.addDays(-1);
gs.addInfoMessage("Original: " + gdt);
gs.addInfoMessage("Added 10 days: " + gdtAdd);
gs.addInfoMessage("Removed 1 day: " + gdtRemove);
Regards.
Ehab Pilloor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2025 02:41 AM
Updated Code:
var gdt = new GlideDateTime("2025-07-03 08:03:35");
gs.addInfoMessage(gdt);
gdt.addDays(10)//add 10 days to current date time
gs.addInfoMessage(gdt); //adding 10 days
gdt.addDays(-1); //remove 1 days from current date time
gs.addInfoMessage(gdt);
Basically you can add directly to the gdt reference instead of storing it into local variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2025 02:49 AM
@ServiceNow Use6, you had it almost right, pelase refer to this:
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */