- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 01:29 PM - edited 01-31-2023 01:35 PM
Hi,
I have a question about converting the date from ISO 8601 format to yyyy-MM-dd HH:mm:ss
I use this script in background script from community but I'm getting as an output
2018-8-18 6:39:51
How to modify script to get default ServiceNow format yyyy-MM-dd HH:mm:ss (for example 2018-09-18 06:39:51)
var source_iso = '2018-09-18T06:39:51Z';
if(source_iso.indexOf(".")==-1)
{
source_iso =source_iso.replace("Z", ".000Z");
}
var d = new Date(source_iso);
var day= d.getUTCDate();
var month=d.getUTCMonth();
var year=d.getUTCFullYear();
var hours=d.getUTCHours();
var minutes=d.getUTCMinutes();
var seconds=d.getUTCSeconds();
var userDateFormat = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
gs.info(userDateFormat)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 06:35 PM
Hi @miro2 ,
I am not good with the ISO 8601 time format however I came up with one code which will help in converting the date to the ServiceNow standard format.
Please try the code below.
var source_iso = '2018-09-18T06:39:51Z';
source_iso = source_iso.replace("T"," ");
var gdt = new GlideDateTime(source_iso);
gs.info(gdt);
if the source_iso will be in the same format every time we can use this code.
Try this and let me know if it works out for you.
Mark Helpful and Accept the solution if it helps in solving your Problem.
Regards,
Johns

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 04:30 PM
Hi,
Just add the line in bold:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 12:37 PM
Hi @AkshatRastogi
I just noticed that in output month is August, not September (2018-09-18T06:39:51Z')
Do you know why?
background script output:
2018-08-18 06:39:51
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 05:31 PM
getUTCMonth returns month from 0 to 11 not 1 to 12.
add 1 month atfer getUTCMonth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 06:03 AM
Thanks for clarification