@serenity-is/corelib / formatDate

Function: formatDate()

formatDate(d, format?, locale?): string

Defined in: src/base/formatting.ts:638

Formats a date using the specified format string and optional culture. Supports .NET style format strings including custom formats. See .NET documentation for supported formats.

Parameters

d

the date to format. If null, it returns empty string.

string | Date

format?

string

the format string to use. If null, it uses the current culture's default format. 'G' uses the culture's datetime format. 'g' uses the culture's datetime format with secs removed. 'd' uses the culture's date format. 't' uses the culture's time format. 'u' uses the sortable ISO format with UTC time. 'U' uses the culture's date format with UTC time.

locale?

Locale

the locale to use

Returns

string

the formatted date

Examples

// returns "2019-01-01"
formatDate(new Date(2019, 0, 1), "yyyy-MM-dd");
// returns "2019-01-01 12:00:00"
formatDate(new Date(2019, 0, 1, 12), "yyyy-MM-dd HH:mm:ss");
// returns "2019-01-01 12:00:00.000"
formatDate(new Date(2019, 0, 1, 12), "yyyy-MM-dd HH:mm:ss.fff");
// returns "2019-01-01 12:00:00.000 AM"
formatDate(new Date(2019, 0, 1, 12), "yyyy-MM-dd HH:mm:ss.fff tt");