Solved: How to convert ServiceNow ticket created date in t... - ServiceNow Community

How to convert ServiceNow ticket created date in to ISO 8601 format?

sri26
Giga Contributor

Hi Team,

I have a custom table where i have mapped ticket created field (sys_created_on) field from both RITM and Incident table.

Now the ticket created field is in "2020-09-18 07:41:40" format.

I need to convert this in ISO 8601 format  (eg: 2012-04-23T18:25:43.511Z ).

I tried with different options, but its not working.

Can anyone please help me here?

 

Thanks in advance,

Sri

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Most methods uses package to use Java packages but this does not work with scoped applications.

Following will convert the time in yyyy-mm-dd hh:MM:ss format to ISO 8601 format.

var original = '2020-09-18 07:41:40';
var dt = original.split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(f[0], f[1]-1, f[2], t[0], t[1], t[2]);

var isoDate = event.toISOString();
gs.info('iso date:' + isoDate);

View solution in original post

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Most methods uses package to use Java packages but this does not work with scoped applications.

Following will convert the time in yyyy-mm-dd hh:MM:ss format to ISO 8601 format.

var original = '2020-09-18 07:41:40';
var dt = original.split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(f[0], f[1]-1, f[2], t[0], t[1], t[2]);

var isoDate = event.toISOString();
gs.info('iso date:' + isoDate);

Hi Hozawa,

 

Thanks a lot, i tested from background script,

Its working.

I can now add this peice of code in Business rules when new ritm's or incident created/inserted, it works,

 

Can you please help me on how to add this code while i transforming from RITM/Inc to my custom table.

Can you please share before transform script on same logic please, it would be really appreciated!

 

Thanks in advance,

Sri

 

Jon G1
Kilo Sage

In case anyone else comes across this in a google search like I did:

 

I found this post while searching for a similar answer. I came up with an alternative solution though.  Instead of deconstructing and reconstructing the date string, you can use the GlideDateTime millisecond value:

//Assuming we have 'current' as an incident gliderecord
var incCreatedOn = current.getValue('sys_created_on');
var gdt = new GlideDateTime(incCreatedOn);
var createdOnDate = new Date(gdt.getNumericValue());
var isoDate = createdOnDate.toISOString();

gs.info(isoDate);

//Or if you really wanted to cut it down
var gdt = new GlideDateTime(current.getValue('sys_created_on'));
var isoDate = new Date(gdt.getNumericValue()).toISOString();

 

For me, it is easier to parse the code above because you can more readily see what's happening at each step and don't have to worry about mistyping an array value or forgetting to adjust the month index for Date. It also gives more flexibility if you need to pull date/time in other formats simultaneously since you've already got your GlideDateTime ready to go.  This could be useful if you needed both the ISO time and a human-readable time in the correct time zone (gdt.getDisplayValue()).

 

Hopefully this is helpful to the next person google points this way :).

Nice one! Thanks!