Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to convert Date Time field value to ISO format value

Sanjay34
Tera Contributor

I have written below script on the "sn_customerservice_case" table to convert date time field value to ISO format.

var updated = current.getDisplayValue('sys_updated_on');    
    var dt = updated.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]);    
    gs.log("sanjay event " + event);
    var isoDate = event.toISOString();

In the log it is giving the result as "sanjay event Invalid Date". Please help to provide the solution.

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sanjay,

Did you add gs.info() to debug

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I have applied gs.info for each variable. I getting below values in logs :

sanjay updated 22/08/2022 04:47:39

sanjay dt 22/08/2022,04:47:39
sanjay f 22/08/2022

sanjay t 04,47,39

sanjay event Invalid Date

Please refer the above script for variables values.

Regards,
Sanjay

Hi,

try this

var gdt = new GlideDateTime(current.sys_updated_on);

var dt = gdt.getValue().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(isoDate);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Using above script I am able to get the output value as 
"2022-08-22T15:34:29.000Z"

Now I need to convert to the above value as below 
20220822T153439Z

could you please help me on this part.

Regards,
Sanjay

Hi,

glad to know that my script worked.

The script I shared generates date/time in actual ISO 8601. why to remove the - and :

if you still require then use this

var gdt = new GlideDateTime(current.sys_updated_on);

var dt = gdt.getValue().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 str = event.toISOString();

str = str.replaceAll("-","").replaceAll(":","").replace(".000","");

gs.info(str);

Please mark my response as correct and helpful to close the thread.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader