The Date object to format
The format pattern using Unicode tokens (e.g., "yyyy-MM-dd HH:mm:ss")
Optional
locale: LocaleOptional localization object for locale-specific formatting
Formatted date string
const date = new Date(2024, 0, 15, 14, 30, 45, 123);
// Basic date formatting
format(date, "yyyy-MM-dd"); // "2024-01-15"
format(date, "dd/MM/yyyy"); // "15/01/2024"
// Date and time combined
format(date, "yyyy-MM-dd HH:mm:ss"); // "2024-01-15 14:30:45"
format(date, "dd/MM/yyyy HH:mm"); // "15/01/2024 14:30"
// 12-hour format with AM/PM
format(date, "h:mm a"); // "2:30 PM"
format(date, "hh:mm:ss a"); // "02:30:45 PM"
// With milliseconds
format(date, "HH:mm:ss.SSS"); // "14:30:45.123"
// Year variations
format(date, "y"); // "2024"
format(date, "yy"); // "24"
format(date, "yyy"); // "024"
format(date, "yyyy"); // "2024"
// Month variations
format(date, "M"); // "1"
format(date, "MM"); // "01"
format(date, "MMM"); // "Jan" (abbreviated)
format(date, "MMMM"); // "January" (full name)
format(date, "MMMMM"); // "J" (narrow)
// Weekday formatting
format(date, "E"); // "Mon"
format(date, "EEE"); // "Mon"
format(date, "EEEE"); // "Monday"
format(date, "EEEEE"); // "M"
// Day period variations
format(date, "h:mm a"); // "2:30 PM"
format(date, "h:mm aa"); // "2:30 PM"
format(date, "h:mm aaa"); // "2:30 PM"
format(date, "h:mm aaaa"); // "2:30 P.M."
format(date, "h:mm aaaaa"); // "2:30 p"
// Era formatting
const bcDate = new Date(-100, 0, 1); // 101 BC
format(bcDate, "yyyy G"); // "0101 BC"
format(bcDate, "yyyy GGGG"); // "0101 Before Christ"
format(bcDate, "yyyy GGGGG"); // "0101 B"
// Day of year
format(date, "DDD"); // "015" (15th day of year)
// Literal text in pattern (enclosed in quotes)
format(date, "'Year' yyyy', Month' MM"); // "Year 2024, Month 01"
// Escaped single quote in pattern
format(date, "'It''s' yyyy"); // "It's 2024"
// Complex formatting
format(date, "EEEE, MMMM dd, yyyy 'at' h:mm a");
// "Monday, January 15, 2024 at 2:30 PM"
// ISO 8601-like format
format(date, "yyyy-MM-dd'T'HH:mm:ss"); // "2024-01-15T14:30:45"
// Localized formatting (English)
import { enUS } from "./i18n/en-US";
format(date, "MMMM dd, yyyy", enUS); // "January 15, 2024"
// Localized formatting (Japanese)
import { ja } from "./i18n/ja";
format(date, "yyyy'年'M'月'd'日'", ja); // "2024年1月15日"
Supported Format Tokens:
Formatting Behavior:
Year Formatting:
12-Hour Format:
Millisecond Formatting:
Day of Year Formatting:
Localization:
Common Patterns:
Performance Considerations:
Relationship with parse():
Format a Date object according to a format pattern.
This function converts Date objects to formatted strings using Unicode format tokens. Supports the same token syntax as the parse() function for consistency. Includes support for localized formatting of month names, weekdays, day periods, and eras.