The Date object to be formatted.
A predefined string literal specifying the desired output format. Examples include "YYYY-MM-DD", "Month DD, YYYY", "HH:MM period", etc.
Optional settings to customize the formatting behavior.
Optionalabbreviations?: booleanIf true, month and day names will be abbreviated (e.g., "Jan.", "Mon."). Defaults to false.
OptionalnoZeroPadding?: booleanIf true, single-digit days and months will not be padded with a leading zero (e.g., "1" instead of "01"). Defaults to false.
Optionalstyle?: "cbc" | "rc"The linguistic style for formatting. Use "cbc" for English (default) or "rc" for French. This affects month and day names, and time representations.
OptionalthreeLetterMonth?: booleanIf true, month abbreviations will be three letters (e.g., "Jan", "Feb"). Defaults to false.
OptionaltimeZone?: Specifies a time zone for formatting. Accepts standard IANA time zone names (e.g., "America/New_York") or specific Canadian time zones. If utc is true, this option is ignored.
Optionalutc?: booleanIf true, the date will be formatted in UTC (Coordinated Universal Time). Defaults to false.
The formatted date string.
// Basic usage: Format a date in default English style.
const date = new Date("2023-01-01T01:35:00.000Z");
const formatted = formatDate(date, "Month DD, YYYY, at HH:MM period", { utc: true });
console.log(formatted); // "January 1, 2023, at 1:35 a.m."
// Formatting in French style with abbreviations.
const frenchFormatted = formatDate(date, "Month DD, YYYY, at HH:MM period", {
style: "rc",
abbreviations: true,
utc: true
});
console.log(frenchFormatted); // "1 janv. 2023 à 1 h 35"
Formats a
Dateobject into a human-readable string based on a specified format, style, and time zone. This function provides flexible date and time formatting options, including support for UTC, different linguistic styles (English/French), and various display preferences like abbreviations and zero-padding.