formatToParts Rebuilds ISO Stamps
DateTimeFormat.formatToParts splits a formatted date into typed fields that reassemble into ISO stamps, and resolvedOptions reports the chosen locale rules.
Code
const stamp = new Date('2026-11-23T18:05:00Z');
const parts = new Intl.DateTimeFormat('en-GB', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', timeZone: 'UTC',
}).formatToParts(stamp);
const byType = Object.fromEntries(parts.filter((p) => p.type !== 'literal').map((p) => [p.type, p.value]));
console.log(JSON.stringify(byType));
console.log('rebuilt ISO:', `${byType.year}-${byType.month}-${byType.day}T${byType.hour}:${byType.minute}Z`);
// resolvedOptions 暴露实际生效的区域、日历与计数系统
const resolved = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeZone: 'UTC' }).resolvedOptions();
console.log('resolved:', resolved.locale, resolved.dateStyle, resolved.calendar, resolved.numberingSystem);Output
Captured from a sandboxed run of this snippet in the build pipeline.
{"day":"23","month":"11","year":"2026","hour":"18","minute":"05"}
rebuilt ISO: 2026-11-23T18:05Z
resolved: en-US medium gregory latnA guided pass through the code
All of the state in this file sits in top-level bindings: stamp, parts, byType, resolved.
Caveats
- Installing is unnecessary — the snippet never steps outside the standard library.
Applying it
The run emits 2 labeled sections — rebuilt ISO:, resolved: — one per step.
Statements that matter
const resolved = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeZone: 'UTC' }).resolvedOptions();
const byType = Object.fromEntries(parts.filter((p) => p.type !== 'literal').map((p) => [p.type, p.value]));
const parts = new Intl.DateTimeFormat('en-GB', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', timeZone: 'UTC',
}).formatToParts(stamp);
Interactive Tools for This Topic
ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.ISO Week CalculatorFind the ISO 8601 week number, ISO week-year, week start and end dates, and day of year for any calendar date, with strict date validation.
FAQ
- Do I need to install anything before running this?
- No — every API used here ships with the language runtime, so there is nothing to install. Its state lives in module-level bindings: `stamp`, `parts`, `byType`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.