Date and Time
Date manipulation patterns in SSJS — formatting, calculating differences, timezone handling, and building date ranges.
Claims checked
Test scripts included
Get Current Date/Time
var now = Platform.Function.Now(); // "3/30/2026 10:15:00 AM" (SFMC server time)
function formatDate(dateString,dateFormat,timeFormat,isoLocale) {
Platform.Variable.SetValue("@formatDate_string",dateString);
Platform.Variable.SetValue("@formatDate_date",dateFormat);
Platform.Variable.SetValue("@formatDate_time",timeFormat);
Platform.Variable.SetValue("@formatDate_iso",isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
// Format for display — the 3rd argument is timeFormat, the locale is 4th
var displayDate = formatDate(now, "MM/DD/YYYY", "", "en-US");
var isoDate = formatDate(now, "YYYY-MM-DD", "", "en-US");
var fullDateTime = formatDate(now, "MM/DD/YYYY", "HH:mm:ss", "en-US");
// For cookies/HTTP headers
var httpDate = formatDate(now, "ddd, DD MMM YYYY", "HH:mm:ss", "en-US") + " GMT";
Show test script
<script runat="server">
/*
* Chapter: Get Current Date/Time
* Proves:
* 1. Now + FormatDate via TreatAsContent.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
function formatDate(dateString, dateFormat, timeFormat, isoLocale) {
Platform.Variable.SetValue("@formatDate_string", dateString);
Platform.Variable.SetValue("@formatDate_date", dateFormat);
Platform.Variable.SetValue("@formatDate_time", timeFormat);
Platform.Variable.SetValue("@formatDate_iso", isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
var now = Platform.Function.Now();
var displayDate = formatDate(now, "MM/DD/YYYY", "", "en-US");
assert("Now usable", now !== null && now !== undefined ? "ok" : "bad", "ok");
assert("displayDate non-empty", ("" + displayDate).length > 0 ? "true" : "false", "true");
</script>
Date Arithmetic
var now = Platform.Function.Now();
function dateAdd(timestamp,intervalToAdd,intervalType) {
Platform.Variable.SetValue("@dateAdd_ts",timestamp);
Platform.Variable.SetValue("@dateAdd_add",intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type",intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
// Add/subtract time
var tomorrow = dateAdd(now, 1, "D");
var nextWeek = dateAdd(now, 7, "D");
var nextMonth = dateAdd(now, 1, "M");
var nextYear = dateAdd(now, 1, "Y");
var inTwoHours = dateAdd(now, 2, "H");
var tenMinutesAgo = dateAdd(now, -10, "MI");
// Date parts: Y=year, M=month, D=day, H=hour, MI=minute, S=second
Show test script
<script runat="server">
/*
* Chapter: Date Arithmetic
* Proves:
* 1. DateAdd via TreatAsContent returns string.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
function dateAdd(timestamp, intervalToAdd, intervalType) {
Platform.Variable.SetValue("@dateAdd_ts", timestamp);
Platform.Variable.SetValue("@dateAdd_add", intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type", intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
var tomorrow = dateAdd(Platform.Function.Now(), 1, "D");
assert("tomorrow non-empty", ("" + tomorrow).length > 0 ? "true" : "false", "true");
</script>
Cookie Expiry Date
function dateAdd(timestamp,intervalToAdd,intervalType) {
Platform.Variable.SetValue("@dateAdd_ts",timestamp);
Platform.Variable.SetValue("@dateAdd_add",intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type",intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
function formatDate(dateString,dateFormat,timeFormat,isoLocale) {
Platform.Variable.SetValue("@formatDate_string",dateString);
Platform.Variable.SetValue("@formatDate_date",dateFormat);
Platform.Variable.SetValue("@formatDate_time",timeFormat);
Platform.Variable.SetValue("@formatDate_iso",isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
// Cookie expiry 30 days from now
var expiry = formatDate(
dateAdd(Platform.Function.Now(), 30, "D"),
"ddd, DD MMM YYYY",
"HH:mm:ss",
"en-US"
) + " GMT";
Platform.Response.SetCookie("session", token, expiry, true);
Show test script
<script runat="server">
/*
* Chapter: Cookie Expiry Date
* Proves:
* 1. formatDate(dateAdd(...)) for cookie expiry string.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
function dateAdd(timestamp, intervalToAdd, intervalType) {
Platform.Variable.SetValue("@dateAdd_ts", timestamp);
Platform.Variable.SetValue("@dateAdd_add", intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type", intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
function formatDate(dateString, dateFormat, timeFormat, isoLocale) {
Platform.Variable.SetValue("@formatDate_string", dateString);
Platform.Variable.SetValue("@formatDate_date", dateFormat);
Platform.Variable.SetValue("@formatDate_time", timeFormat);
Platform.Variable.SetValue("@formatDate_iso", isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
var expiry = formatDate(dateAdd(Platform.Function.Now(), 30, "D"), "ddd, DD MMM YYYY", "HH:mm:ss", "en-US");
assert("expiry non-empty", ("" + expiry).length > 5 ? "true" : "false", "true");
</script>
Timezone Conversion
function formatDate(dateString,dateFormat,timeFormat,isoLocale) {
Platform.Variable.SetValue("@formatDate_string",dateString);
Platform.Variable.SetValue("@formatDate_date",dateFormat);
Platform.Variable.SetValue("@formatDate_time",timeFormat);
Platform.Variable.SetValue("@formatDate_iso",isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
// Convert SFMC server time to subscriber's local time
var serverTime = Platform.Function.Now();
var localTime = Platform.Function.SystemDateToLocalDate(serverTime);
var localDisplay = formatDate(localTime, "MM/DD/YYYY", "h:mm a", "en-US");
Write("Your local time: " + localDisplay);
Show test script
<script runat="server">
/*
* Chapter: Timezone Conversion
* Proves:
* 1. SystemDateToLocalDate / LocalDateToSystemDate typeof.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
assert("SystemDateToLocalDate", typeof Platform.Function.SystemDateToLocalDate, "clrmethodinfo");
assert("LocalDateToSystemDate", typeof Platform.Function.LocalDateToSystemDate, "clrmethodinfo");
</script>
Date Range Filter
function dateAdd(timestamp,intervalToAdd,intervalType) {
Platform.Variable.SetValue("@dateAdd_ts",timestamp);
Platform.Variable.SetValue("@dateAdd_add",intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type",intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
function formatDate(dateString,dateFormat,timeFormat,isoLocale) {
Platform.Variable.SetValue("@formatDate_string",dateString);
Platform.Variable.SetValue("@formatDate_date",dateFormat);
Platform.Variable.SetValue("@formatDate_time",timeFormat);
Platform.Variable.SetValue("@formatDate_iso",isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
// Get records from the last 7 days
var sevenDaysAgo = formatDate(
dateAdd(Platform.Function.Now(), -7, "D"),
"MM/DD/YYYY",
"HH:mm:ss"
);
// Use with Platform.Function (filter by date string comparison)
var recentRows = Platform.Function.LookupRows("Events", "CreatedAt", sevenDaysAgo);
// Note: This is an exact match, not a range. Use WSProxy for range queries.
// Range query using WSProxy
var proxy = new Script.Util.WSProxy();
var result = proxy.retrieve(
"DataExtensionObject[Events_Key]",
["Email", "EventType", "CreatedAt"],
{
LeftOperand: {
Property: "CreatedAt",
SimpleOperator: "greaterThan",
Value: sevenDaysAgo
},
LogicalOperator: "AND",
RightOperand: {
Property: "CreatedAt",
SimpleOperator: "lessThan",
Value: formatDate(Platform.Function.Now(), "MM/DD/YYYY", "HH:mm:ss")
}
}
);
Show test script
<script runat="server">
/*
* Chapter: Date Range Filter
* Proves:
* 1. dateAdd -7 D for range lower bound.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
function dateAdd(timestamp, intervalToAdd, intervalType) {
Platform.Variable.SetValue("@dateAdd_ts", timestamp);
Platform.Variable.SetValue("@dateAdd_add", intervalToAdd);
Platform.Variable.SetValue("@dateAdd_type", intervalType);
return Platform.Function.TreatAsContent("%%=DateAdd(@dateAdd_ts, @dateAdd_add, @dateAdd_type)=%%");
}
var from = dateAdd(Platform.Function.Now(), -7, "D");
assert("from non-empty", ("" + from).length > 0 ? "true" : "false", "true");
</script>
Format for DE Storage vs Display
var now = Platform.Function.Now();
function formatDate(dateString,dateFormat,timeFormat,isoLocale) {
Platform.Variable.SetValue("@formatDate_string",dateString);
Platform.Variable.SetValue("@formatDate_date",dateFormat);
Platform.Variable.SetValue("@formatDate_time",timeFormat);
Platform.Variable.SetValue("@formatDate_iso",isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
// DE-compatible format (consistent, sortable)
var deFormat = formatDate(now, "MM/DD/YYYY","HH:mm:ss", "en-US");
// User-friendly display
var displayFormat = formatDate(now, "MMMM D, YYYY", "", "en-US");
// "March 30, 2026"
// ISO 8601 (for APIs, JSON)
var isoFormat = formatDate(now, "YYYY-MM-DD", "THH:mm:ss", "en-US");
// "2026-03-30T10:15:00"
Show test script
<script runat="server">
/*
* Chapter: Format for DE Storage vs Display
* Proves:
* 1. ISO-like FormatDate output non-empty.
* EXPECTED OUTPUT: every line starts with PASS.
*/
function assert(id, actual, expected) {
Platform.Response.Write((actual === expected ? "PASS " : "FAIL ") + id + " -> [" + actual + "]\n");
}
function assertThrows(id, fn) {
var threw = false, msg = "";
try { fn(); } catch (ex) { threw = true; msg = "" + ex.message; }
Platform.Response.Write((threw ? "PASS " : "FAIL ") + id + " -> " + (threw ? "threw: " + msg : "did NOT throw") + "\n");
}
function typeOfThunk(fn) {
try { return "" + fn(); } catch (ex) { return "THREW:" + ("" + ex.message); }
}
function formatDate(dateString, dateFormat, timeFormat, isoLocale) {
Platform.Variable.SetValue("@formatDate_string", dateString);
Platform.Variable.SetValue("@formatDate_date", dateFormat);
Platform.Variable.SetValue("@formatDate_time", timeFormat);
Platform.Variable.SetValue("@formatDate_iso", isoLocale);
return Platform.Function.TreatAsContent("%%=FormatDate(@formatDate_string, @formatDate_date, @formatDate_time, @formatDate_iso)=%%");
}
var iso = formatDate(Platform.Function.Now(), "YYYY-MM-DD", "", "en-US");
assert("iso non-empty", ("" + iso).length >= 8 ? "true" : "false", "true");
</script>