-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preferred DateTimeFormat invocation to get YYYY-MM-DD formatting #891
Comments
@paulirish what's your use case? Are you looking for a "human text" (based on locale) or simply an ISO date-only format? For an ISO date-only format, I think you'd be interested on https://tc39.es/proposal-temporal/docs/plaindate.html#toString |
I have ISO timestamps and I want to format them for technical/developer humans. ;) If the user is in PDT (as I am)...
|
+1 on using Temporal once available. There are also some other open issues/proposals that could make this functionality available: Stable Formatting Proposal: https://github.com/tc39/proposal-stable-formatting I separately want the |
Looks like PlainDate explicitly doesn't handle timezone adjustments. The closest I can get with Temporal is this: const opts = new Intl.DateTimeFormat().resolvedOptions();
Temporal.Instant.from('2024-05-16T01:30:00.000Z').toZonedDateTime(opts).toString().replace(/T.*/, '') // '2024-05-15' Is that about right? |
Here's a Temporal-only solution. Was this what you're looking for? Temporal.Instant.from('2024-05-16T01:30:00.000Z')
.toZonedDateTimeISO(Temporal.Now.timeZoneId())
.toPlainDate();
// => 2024-05-15 |
@justingrant yeah that's lovely. Thank you Shane, I'll keep an eye on the |
For anyone reading this issue in the future wondering why there's an
The intent of adding that By requiring developers to look up the method in the docs to understand this
This API naming decision was difficult. It was the most contentious argument in Temporal's 7+ year history. The two positions could be summarized as:
The current API is a compromise between these two positions: ergonomic, ISO-only APIs are available, but they require learning about and using an |
Before Temporal, there's the obvious but verbose const zeroPad = (val, len) => (val + "").padStart(len, "0");
const toLocalDateString = date =>
`${date.getFullYear()}-${zeroPad(date.getMonth() + 1, 2)}-${zeroPad(date.getDate(), 2)}`; or the more concise and clever const toLocalDateString = date =>
new Date(date - date.getTimezoneOffset() * 60_000).toISOString().replace(/T.*/, ""); |
As the primary stakeholder who advocated for it, this doesn't capture my position. The intent was that every call site should be explicit when introducing a calendar system, because implicit calendars can be a source of i18n bugs. Many users have a non-Gregorian system like Buddhist, Hebrew, or Islamic in their e-calendar, and the calendar parameter is required in order to make operations such as "add 1 month" behave correctly. It wasn't about educating developers of the existence of non-Gregorian calendars; it was about preventing i18n bugs. |
For a developer-centric audience, I want dates formatted in YYYY-MM-DD. (But, I don't want ISO/UTC times.)
There are multiple ways to do it, but the safer and less-risky options are pretty verbose. :/
There are a handful of locales that where CLDR defines that formatting as preferred.. eg:
en-CA
,fr-CA
,lt-LT
,sv-SE
While short, it feels inappropriate to use a specific locale only because it happens to match YYYY-MM-DD. Is there much of a risk of this changing?
The more verbose option is
formatToParts
:It gets the job done (and probably won't break even if CLDR changes some fundamental en-US things), but it's not not so elegant.
And there's plenty of more abbreviated options like these, but... they do rely on the current CLDR. (So it's probably fine for the next 20 years… who knows past that. :)
What's best practice?
I'm definitely not requesting a feature to specify YYYY-MM-DD.
I'm curious how i18n experts would solve this very basic, but recurring, question. :)
The text was updated successfully, but these errors were encountered: