Parameters

date Date The date to format. Often `getCurrentDate()` or `new Date()`.
format string A Campaign date mask (see tokens below).

Returns

string The formatted date.

Examples

Formats a date into a string using a Campaign date mask β€” for file names, folder paths, log messages and labels. To go the other way, from a stored date string back into a Date, use πŸ“• parseTimeStamp.

As a general rule, it is not the right tool for writing a date into the database or an XTK condition, nor for displaying a date in the user’s locale (see When not to use below).

Format tokens

Token Meaning For 2026-05-09 17:08:05
%4Y year, 4 digits 2026
%2M month, 2 digits 05
%2D day, 2 digits 09
%D day, no padding 9
%2H hour (24h), 2 digits 17
%2N minutes, 2 digits 08
%2S seconds, 2 digits 05
%I Hour 12h (1-12) 5
%P AM/PM PM
%B month name May
%A weekday name Saturday
%J Day of the year (1-366) Β 
%W Week of the year Β 

A leading number adds padding (%2N > 02); a trailing l displays the full form (%Bl, %Al > May, Saturday).

Common patterns

A full timestamp:

var stamp = formatDate(getCurrentDate(), "%4Y/%2M/%2D %2H:%2N:%2S"); // 2026/05/29 17:45:58

A timestamped file name. Note h/m/s as separators, because : is invalid in file names:

var fileName = "export_" + formatDate(new Date(), "%4Y-%2M-%2DT%2Hh%2Nm%2Ss") + ".csv";
// export_2026-05-29T17h45m58s.csv

A numeric date key, handy for comparisons:

var ymd = parseInt(formatDate(new Date(), "%4Y%2M%2D"), 10); // 20260529

To put a current date into SQL, don’t format and concatenate β€” use the $(curdate) placeholder of πŸ“• sqlExec.

See also

  • πŸ“• getCurrentDate - Return the current server date and time as a JavaScript Date. Use it instead of new Date() for "now".