as i start this article, i have a confusion to say . i really hate working with date:).however,if you are dealing a lot with custom integration you probably notice that working with date in ServiceNow is more a complex.
in some cases, like for example sending date in SOAP Message, you need to follow ISO-8601 standard . The example below,shows how to convert GlideDate to ISO 8601 with two different format
2014-09-02T11:28:56+00:00
2014-09-02T11:28:56Z
function ConvertToISO8601(passdate){
// java class concrete class for formatting and parsing dates in a locale-sensitive manner
var SimpleDateFormat = Packages.java.text.SimpleDateFormat;
var TimeZone = Packages.java.util.TimeZone;
// java class lets you manipulate dates in a system independent way
var Date = Packages.java.util.Date;
var gdt =passdate.getGlideObject();
//convert the date to milliseconds
var date = new Date(gdt.getNumericValue());
var format = “yyyy-MM-dd’T’HH:mm:ssZ”;
// set the time zone here
tz = TimeZone.getTimeZone(‘US/Mountain’);
var f = new SimpleDateFormat(format);
f.setTimeZone(tz);
var formatted = f.format(date); // return f.format(date) would be in this format 2014-09-02T11:28:56+00:00
return formatted.substring(0, 19) + ‘Z’; //would be in this format 2014-09-02T11:28:56Z
}