- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 05:44 AM
I have date time value "10 Jul 2018 12:30:00". I am trying to add 7 days to it. I am using the GlideDateTime(<date_string>). I am unable to get the object so that i can add days to to it. If i use the gs.nowNoTZ() function and pass it as input to GlideDateTime it is working. I found that gs.nowNoTZ() give date in the format 2018-07-04 06:48:16. Due to this difference i am unable to get the result. My input is "10 Jul 2018 12:30:00" How can i change this format?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 05:46 PM
To change the format, you first need to set up GlideDateTime using your current format
var date = '10 Jul 2018 12:30:00';
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();
gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ
gs.addInfoMessage(gdt.getDisplayValue()); // Output time in current TZ
2018-07-10 12:30:00 PM |
Once your GlideDateTime object has the correct time, you can manipulate it as you need.
gdt.addDays(7);
gs.addInfoMessage(gdt.getDisplayValue());
2018-07-17 12:30:00 PM |
And back again to your original format, with the added 7 days
var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();
gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));
17 Jul 2018 12:30:00 |
See my blog for more examples.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 05:46 PM
To change the format, you first need to set up GlideDateTime using your current format
var date = '10 Jul 2018 12:30:00';
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();
gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ
gs.addInfoMessage(gdt.getDisplayValue()); // Output time in current TZ
2018-07-10 12:30:00 PM |
Once your GlideDateTime object has the correct time, you can manipulate it as you need.
gdt.addDays(7);
gs.addInfoMessage(gdt.getDisplayValue());
2018-07-17 12:30:00 PM |
And back again to your original format, with the added 7 days
var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();
gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));
17 Jul 2018 12:30:00 |
See my blog for more examples.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 09:13 PM
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2018 10:55 PM
Please mark as correct if this has answered you question 🙂
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2018 10:43 AM
Hi Paul,
we have a similar requirement for one of a catalog item, where the date format is "dd-mmm-yy" - 10-jul-18
but it has to display as "dd-mmm-yyyy" - 10-jul-2018 and the same to be displayed in RITM AND TASK
can you provide the code for this
and it should not affect the system date format
thanks in advance