SFMC SSJS Function to Convert a Date Object to ISO Format
What is ISO-8601 DateTime Format?
ISO 8601 is an international standard covering the worldwide exchange and communication of date and time related data.
Whats the reason for this post?
Newer versions of JavaScript have a toISOString
method. Meanwhile Salesforce Marketing Cloud’s version of Server Side JavaScript is based on ECMA 3 which is an older version lacking many new advancements. One such lacking feature is the toISOString
method, so we need to get crafty and write some code to replicate this function.
In the pages below I will provide a brief overview of the missing JS features, recap UTC and finally present 3x SFMC SSJS friendly alternatives.
Date.prototype.toISOString()
Newer versions of JavaScript have a toISOString
method that gives a datetime in ISO-8601 format. However, note that the output this gives is a string in UTC format, YYYY-MM-DDTHH:mm:ss.sssZ
(so this version does not give a timezone offset in hours and minutes).
The letter Z is used as a suffix to denote a time being in the Zulu Time Zone, which is just another name for UTC+0. You can also think of it like this: the Z stands for the “Zero timezone,” or the “Zero UTC offset” as it is offset by 0 from the Coordinated Universal Time (UTC). 09:30 UTC
is therefore represented as 09:30Z
.
Next I will present 3 different ways to format your date-time variable to a string that complies with the ISO-8601 standard.
Method 1
Use AMPscript’s enhanced date and time manipulation functionality. Run the ampscript %%=FormatDate(Now(),'iso')=%%
code in the SSJS runtime.
var isoTime = Platform.Function.TreatAsContent("\%\%=FormatDate(Now(),'iso')=\%\%");
// OUTPUT:
// 2022–12–21T20:50:41.4507159–06:00
Method 2
Use Format() function. Does not require you to load the Core library. But note it will omit milliseconds.
var d = new Date();
var isoTime = Format(d,'yyyy-MM-ddTHH:mm:sszzz')
// OUTPUT:
// 2022–12–21T20:50:41–06:00
Method 3
Write a custom function to do the heavy lifting to format our datetime like: YYYY-MM-DDTHH:mm:ss.sssZZZ
function formatToIsoString(date) {
var tzo = -date.getTimezoneOffset();
var dif = tzo >= 0 ? '+' : '-';
var pad = function(number) {
return (number < 10 ? '0' : '') + number;
};
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
dif + pad(Math.floor(Math.abs(tzo) / 60)) +
':' + pad(Math.abs(tzo) % 60);
};
// OUTPUT:
// 2022–12–21T20:50:41.451–06:00
Consider throwing the below code into an HTML email and subscriber preview to test the output:
<script language="javascript" runat="server">
var now = Platform.Function.Now()
Platform.Response.Write("now: " + Platform.Function.Stringify(now) + "<br><br>");
var today = new Date()
Platform.Response.Write("today: " + Platform.Function.Stringify(today) + "<br><br>");
var jsFunction = formatToIsoString(now)
Platform.Response.Write("jsFunction: " + jsFunction + "<br><br>");
var ampIsoTime = Platform.Function.TreatAsContent("\%\%=FormatDate(Now(),'iso')=\%\%");
Platform.Response.Write("ampIsoTime: " + ampIsoTime + "<br><br>");
/*===========================================
*
* FUNCTIONS
*
===========================================*/
function formatToIsoString(date) {
var pad = function(number) {
return (number < 10 ? '0' : '') + number;
};
var tzo = -date.getTimezoneOffset();
var dif = tzo >= 0 ? '+' : '-';
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
dif + pad(Math.floor(Math.abs(tzo) / 60)) +
':' + pad(Math.abs(tzo) % 60);
};
/*----------------------------------------------------------------------*/
</script>