Last active
June 4, 2025 09:59
-
-
Save restump/abfad4a5c79d963ce0461ce79208b44d to your computer and use it in GitHub Desktop.
ServiceNow javascript function to convert ISO8601 DateTime strings to GlideDateTime objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* function : ISODateTimeToGlideDateTime() | |
| Convert ISO8601 DateTime string to GlideDateTime object | |
| */ | |
| function ISODateTimeToGlideDateTime(s) { | |
| if ( (s == '') || (s == null) ) { | |
| return null; | |
| } | |
| // GlideDateTime constructor expects date format: 'YY-MM-DD HH::MM::SS' | |
| parts = s.split(/[-TZ:+]/g); | |
| if (parts.length < 6) { | |
| return null; | |
| } | |
| dts = ''; | |
| dts += parts[0] + "-" + parts[1] + "-" + parts[2]; | |
| dts += ' '; | |
| // Handle sub seconds | |
| sec = parts[5].split(/\./); | |
| dts += parts[3] + ":" + parts[4] + ":" + sec[0]; | |
| // Calculate timezone offset in seconds | |
| sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+'; | |
| if ( (parts[6] != null) && (parts[7] != null) ) { | |
| offset = parseInt(parts[6] * 3600 + parseInt(parts[7] * 60)); | |
| offset = 0 + (sign == '-' ? -1 * offset : offset); | |
| } else { | |
| offset = 0; | |
| } | |
| // GlideDateTime object | |
| gdt = new GlideDateTime(dts); | |
| gdt.addSeconds(offset); | |
| return gdt; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment