Intl.DateTimeFormat Formats Dates Per Locale
Render dates through Intl.DateTimeFormat so field order, month names, and range collapsing come from the locale rules instead of hand-written format strings.
Code
// Intl.DateTimeFormat: 日期渲染规则交给区域设置,而不是手写格式串
const launch = new Date('2026-03-14T09:30:00Z');
const en = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'UTC',
});
console.log('en-US :', en.format(launch));
const de = new Intl.DateTimeFormat('de-DE', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'UTC',
});
console.log('de-DE :', de.format(launch));
// formatToParts: 拿到字段分段,自定义组装
const parts = en.formatToParts(launch).filter((p) => p.type !== 'literal');
console.log('parts :', parts.map((p) => p.type + '=' + p.value).join(' '));
// formatRange: 同日的两个时刻自动省略重复部分
const end = new Date('2026-03-14T17:00:00Z');
console.log('range :', en.formatRange(launch, end));Output
Captured from a sandboxed run of this snippet in the build pipeline.
en-US : March 14, 2026 at 9:30 AM
de-DE : 14. März 2026 um 09:30
parts : month=March day=14 year=2026 hour=9 minute=30 dayPeriod=AM
range : March 14, 2026, 9:30 AM – 5:00 PMHow the snippet works
Top-level bindings launch, en, de, parts hold the demo state — nothing sits inside a function scope.
Using it
The demo output is labeled (en-US :, de-DE :, parts :), so every printed line maps back to its originating operation.
Key statements
const parts = en.formatToParts(launch).filter((p) => p.type !== 'literal');
const en = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'UTC',
});
const de = new Intl.DateTimeFormat('de-DE', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'UTC',
});
Interactive Tools for This Topic
Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.Business Days CalculatorCount the weekdays between two dates, excluding weekends and any holidays you list, and see how many calendar and weekend days are involved.Cron Next Runs CalculatorCompute the next scheduled run times for a standard five-field cron expression, supporting ranges, lists, steps, and day-of-week naming semantics.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.
FAQ
- Does it depend on any third-party library?
- No third-party library is referenced; the standard library covers it. Its state lives in module-level bindings: `launch`, `en`, `de`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.