24 ms·
You will still need libraries to do nicely formatted time strings like "next week" or "3 hours ago"
by lxe 6y ago
You will still need libraries to do nicely formatted time strings like "next week" or "3 hours ago"
- runarberg 6y agoLook at `Intl.RelativeTimeFormat`[1] and `Intl.DateTimeFormat`[2]. Regrettably Safari is—yet again—lacking with support, but in the meantime there is a polyfill. 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... 2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- chrismorgan 6y agoI find RelativeTimeFormat to be not as useful as it seems—it’s surprisingly limited in scope and utility. You still have to calculate the magnitude and unit of the distance (“3 hours ago” → (3, 'hour') rather than saying “here’s a Date object that happens to be about three hours in the future, you tell me what unit to show”), and it only supports one unit at a time (“1 day ago” or “27 hours ago”, not anything like “1 day, 3 hours ago”), and it can’t produce anything like “next Tuesday” or “this Friday”. Twice I’ve started with it, twice I’ve tossed it out because in the end it didn’t help my code at all. (Both times I only cared about English, so its replacement which did a better job of formatting values for my situation took pretty much no extra code—for exact equivalence, formatter.format(-n, 'day') for known positive n becomes a simple ternary, `n == 1 ? '1 day ago' : '${n} days ago'` (or if the formatter has the {numeric: 'auto'} option, change '1 day ago' to 'yesterday'); but even with full localisation, I’d still be unlikely to use RelativeTimeFormat.)
- runarberg 6y agoI guess Temporal will help you there[1]: const now = Temporal.now.absolute(); const then = Temporal.Absolute.from( '1989-11-09T22:45:00+0100[Europe/Berlin]', ); const sinceThen = now.difference(then); sinceThen.toLocaleString('en'); 1: https://tc39.es/proposal-temporal/docs/duration.html#toLocaleString https://tc39.es/proposal-temporal/docs/duration.html#toLocal...
- josteink 6y ago> You will still need libraries to do nicely formatted time strings like "next week" or "3 hours ago" If you want those formatted values to be consistent with the rest of your app, you will need this anyway. You app does support localisation right? Also: there’s no guarantee a browser would have the same localisation as your app (for instance German-language browser, English website). You really have to do this 100% yourself in your app if you want the result to look right.
- runarberg 6y agoApps are seldomly translated into my native language, but I do have my browser configured to use my native language, so I often see date values in a different language from the rest of the app, and it doesn’t bother me at all. In fact I hardly notice it.
- jacobr 6y agoYou would do new Intl.DateTimeFormat(siteLocale) and not new Intl.DateTimeFormat(browserLocale) to get consistent formatting.