How to convert Date Time field value to ISO format value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2022 12:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2022 09:33 PM
Hi Sanjay,
Did you add gs.info() to debug
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2022 09:55 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2022 10:26 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 01:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2022 07:49 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader