Introduction

The ISO 8601 format is a widely used standard for representing date and time, particularly useful when exchanging time-related information across systems.

In ServiceNow, you often need to interact with external systems or APIs that use ISO 8601 for date representation. However, ServiceNow mainly uses GlideDateTime objects for handling dates and times. These functions bridge that gap, allowing seamless conversion between the two formats.

An ISO 8601 string typically looks like this: 1977-04-22T06:00:00Z. The Z suffix stands for Zulu time, which corresponds to UTC.

Handling these date strings in ServiceNow can be a bit tricky. In this post, I’ll show two simple functions to convert back and forth between GlideDateTime and ISO 8601 strings.

Convert a GlideDateTime to ISO 8601

/*
@name convertToISODate
@description Convert from GlideDateTime to ISO string
@param {GlideDateTime} [gdt] - GlideDateTime object to be converted
@return {String} Date Time formatted in ISO Date (ISO 8601)
*/
function convertToISODate(gdt){
	try{
		// convert to milliseconds
		var ms = gdt.getNumericValue();

		// convert to ISO 8601 string
		var isoStr = new Date(ms).toISOString();

		return isoStr;
	} catch (exception){
		gs.error('Error converting GlideDateTime to ISO date: ' + exception.message);
		return null;
	}
}

Convert an ISO 8601 string to GlideDateTime

/*
@name convertISODateToGlideDateTime
@description Convert from ISO string to GlideDateTime
@param {String} [isoStr] - ISO string (ISO 8601)
@return {GlideDateTime} GlideDateTime object
*/
function convertISODateToGlideDateTime(isoStr){
	try{
		// convert to JavaScript Date
		var newDate = new Date(isoStr);

		// extract milliseconds
		var ms = newDate.getTime();

		// create GlideDateTime and set value
		var newGdt = new GlideDateTime();
		newGdt.setValue(ms);

		return newGdt;
	} catch (exception){
		gs.error('Error converting ISO date to GlideDateTime: ' + exception.message);
		return null;
	}   
}

Testing the Functions

You can test both functions using a quick script. This script grabs the current date/time, converts it to ISO 8601, and then back to a GlideDateTime object.

var gdt = new GlideDateTime();
var isoStr = convertToISODate(gdt);
var newGdt = convertISODateToGlideDateTime(isoStr);

gs.info("Original GlideDateTime: " + gdt);
gs.info("ISO string: " + isoStr);
gs.info("GlideDateTime converted back from ISO: " + newGdt);

which gives this output:

Original GlideDateTime: 2025-06-13 12:53:27
ISO string: 2025-06-13T12:53:27.359Z
GlideDateTime converted back from ISO: 2025-06-13 12:53:27

Notes

GlideDateTime does not retain milliseconds, so precision beyond seconds is lost in the round-trip conversion. This is because GlideDateTime has a precision of one second. If millisecond precision is crucial for your use case, you may need to store the millisecond information separately or use a different date handling method. For example:

let iso2 = "2025-06-13T13:09:34.000Z"
gs.info(convertISODateToGlideDateTime(iso2)) //--> 2025-06-13 13:09:34
let iso3 = "2025-06-13T13:09:34.999Z"
gs.info(convertISODateToGlideDateTime(iso3)) //--> 2025-06-13 13:09:34

References