Skip to content

polyfill: format for Date and Temporal classes are different in Intl.DateTimeFormat for some locales and options #3303

@fabon-f

Description

@fabon-f
new Intl.DateTimeFormat("ja", { month: "short", day: "numeric" }).format(0);
// "1月1日"
new Intl.DateTimeFormat("ja", { month: "short", day: "numeric" }).format(Temporal.PlainDateTime.from("1970-01-01"));
// "1/1"

(note: V8 also has this problem for some reason. SpiderMonkey doesn't.)

This is because options in Intl.DateTimeFormat and resolvedOptions are not roundtrippable for some cases:

new Intl.DateTimeFormat("ja", { month: "short", day: "numeric" }).resolvedOptions().month;
// "numeric", not "short"
new Intl.DateTimeFormat("ja", { month: "numeric", day: "numeric" });
// different format is chosen when recreating `Intl.DateTimeFormat` from the return value of `resolvedOptions`

I found many combinations of locales and options which produce different result for Temporal classes and Date. I verified this on Node.js and Firefox. Some of them are practically nonsense (e.g. combination of era and month), but still there are bunch of cases.

// Turkish
new Intl.DateTimeFormat('tr', { year: 'numeric', month: 'numeric' }).resolvedOptions().month;
// "2-digit", not "numeric"
new Intl.DateTimeFormat('tr', { year: 'numeric', month: 'numeric' }).format(0);
// "01/1970"
new Intl.DateTimeFormat('tr', { year: 'numeric', month: 'numeric' }).format(Temporal.PlainDateTime.from('1970-01-01'));
// "01.1970"

Script I used to discover these exceptions: https://gist.github.com/fabon-f/d6b22aa3d3c816cabdea2510c8d9b322

Metadata

Metadata

Assignees

No one assigned

    Labels

    ecma402Behavior specific to implementations supporting ecma402non-prod-polyfillTHIS POLYFILL IS NOT FOR PRODUCTION USE!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions