Skip to content

FormatDateTime (date/time formatting)

ts
function FormatDateTime(format: string, date?: Date | any): string;

NOTE

This function is available as of Syncplify Server! v7.1.1. If you are running an older version, upgrade to the most recent version to use it.

Formats a date and time value as a string using simple token-based placeholders. When date is omitted, the current date and time is used.

date may be a native JavaScript Date, a TimeStamp returned by any SyncJS file, VFS or remote client function, or a number read as epoch milliseconds. Passing undefined or null is the same as omitting it, so FormatDateTime(fmt, maybeMissing) formats the current time rather than failing. A value that is supplied but cannot be read as a date raises a catchable error.

Parameters

NameTypeDescription
formatstringA template string containing any combination of the tokens below. All other characters are passed through unchanged.
dateDate, TimeStamp or numberOptional. The moment to format. Defaults to the current moment when omitted, undefined or null.

Tokens

TokenReplaced withExample
YYYY4-digit year2026
MM2-digit month (zero-padded)04
DD2-digit day of month (zero-padded)07
HH2-digit hour in 24-hour format (zero-padded)09
mm2-digit minute (zero-padded)05
ss2-digit second (zero-padded)30

IMPORTANT

Tokens are case-sensitive. MM is the month; mm is the minute. HH is always in 24-hour format.

Returns

The formatted date/time string.

Examples

Timestamp in a filename

ts
var stamp = FormatDateTime('YYYY-MM-DD_HH-mm-ss');
var archive = '/var/outbox/export_' + stamp + '.zip';
// e.g. /var/outbox/export_2026-04-07_09-05-30.zip

Format a timestamp from a file stat

A TimeStamp can be passed straight in; there is no need to convert it first.

ts
var info = StatFileSystemObject('/var/inbox/upload.csv');
var modified = FormatDateTime('YYYY-MM-DD HH:mm:ss', info.TimeStamp);
Log('File last modified: ' + modified);

Converting first with ToDate works equally well, and is what you want when the script also needs to do arithmetic on the value:

ts
var when = ToDate(info.TimeStamp);
Log('File last modified: ' + FormatDateTime('YYYY-MM-DD HH:mm:ss', when));
Log('Age in days: ' + Math.floor((Date.now() - when.getTime()) / 86400000));

IMPORTANT

Earlier builds of the SyncJS engine could not read a TimeStamp here, and quietly fell back to the current time when they failed to. A file last modified years ago formatted as today, with nothing written to the log. It is fixed: a TimeStamp formats correctly, and a value that genuinely is not a date now raises a catchable error rather than returning a plausible wrong answer.

Format a JavaScript Date object

ts
var d = new Date(2026, 3, 7, 9, 5, 30); // April 7, 2026 09:05:30
Log(FormatDateTime('DD/MM/YYYY HH:mm', d));
// Output: 07/04/2026 09:05

NOTE

If you need the current date formatted as a string using only standard JavaScript, you can also use new Date() with its native methods. FormatDateTime is provided as a shortcut for the most common token-based formatting patterns used in file names and log messages.