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.

JavascriptData Fetching

Code

javascriptsnippet
12 lines
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 latn

A 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

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`.