📎 Webclip
Formatting Dates With the DateTimeFormat Object
JavaScript’s Intl.DateTimeFormat formats dates according to locale, so the same Date object can produce different strings for different regions. The constructor accepts one or more locale tags, and options can change the calendar, numbering system, time zone, hour cycle, and which date and time fields appear.
It also exposes methods beyond format(). formatToParts() returns the formatted value as parts, resolvedOptions() shows the locale and options in use, and formatRange() and formatRangeToParts() format a date range in the most concise form for the selected locale.
Reading notes#
- The constructor formats dates for different locales without manual date-string manipulation.
- The first argument can be a locale string or an array of locale strings, with fallback locales on the right.
- Unicode extension keys supported in locale tags include
nu,ca, andhc. nucontrols the numbering system,cacontrols the calendar, andhccontrols the hour cycle.- The second argument accepts an options object for formatting behavior.
localeMatchercan belookuporbest fit.timeZonecan use UTC or IANA time zone names such asAsia/ShanghaiandAmerica/New_York.hour12sets 12-hour or 24-hour output and overrideshc.hourCyclecan beh11,h12,h23, orh24.formatMatchercan bebasicorbest fit.- The required date-time field combinations include year, month, and day, and also hour, minute, and second combinations.
weekday,era,year,month,day,hour,minute,second, andtimeZoneNameeach accept specific display styles.- If all component options are
undefined,year,month, anddaydefault tonumeric. format()returns a locale-formatted date string from aDateobject.formatToParts()returns an array of objects for the formatted parts.resolvedOptions()returns the locale and formatting options computed at initialization.formatRange()formats two dates as a concise range.formatRangeToParts()returns the parts of a formatted date range as an array.- The examples show the same date formatted in English, French Canadian, German, Buddhist calendar English, and Traditional Chinese.
