- 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 05:36 PM
The following worked for me:
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);
gs.info(d);
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 daysStr = '';
if (day < 10)
dayStr = "0"+day;
else
dayStr = day.toString();
var monthStr = '';
if (month < 10)
monthStr = "0"+month;
else
monthStr = month.toString();
var hoursStr = '';
if (hours < 10)
hoursStr = "0"+hours;
else
hoursStr = hours.toString();
var userDateFormat = year+"-"+monthStr+"-"+dayStr+" "+hoursStr+":"+minutes+":"+seconds;
gs.info(userDateFormat);
I get:
*** Script: Mon Sep 17 2018 23:39:51 GMT-0700 (PDT)
*** Script: 2018-08-18 06:39:51
add logic for other values that can be < 10 if you want to use that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 12:45 PM
Hi @Bert_c1
I just noticed that in output month is August, not September (2018-09-18T06:39:51Z')
Do you know why this happens?
output:
*** Script: Mon Sep 17 2018 23:39:51 GMT-0700 (PDT)
*** Script: 2018-08-18 06:39:51
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 05:20 PM
I have no idea why, I haven't found documentation on what you are using (getUTC...). Those seem to return number values. I would do as Johns posted, use ServiceNow Date-time values.
- 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
02-01-2023 12:52 PM
Hi @Johns Marokky
It works, output background script
2018-09-18 06:39:51
I just wonder why I get August, not September with the other community solution from @Bert_c1 and @AkshatRastogi
2018-08-18 06:39:51