6 ms·
The Intl.RelativeTimeFormat API
- wongarsu 8y agoA very nice API, especially with the inclusion of options like style: 'short' (which produces strings like "in 1 mo."). Can't wait until this available in electronjs (sufficient browser support is as always likely to take a while longer)
- bpye 8y agoSeems like something that should be fairly easily polyfilled for web usage?
- dbbk 8y agoThere already are polyfills for it based on the draft spec
- masklinn 8y ago> Can't wait until this available in electronjs Why? Considering the baseline size (both on-disk and in-memory) it's not like relative formatting has much of an impact, unlike genuine websites/web applications.
- tyingq 8y agoPerl's Date::Manip does this sort of thing too. Some examples: https://manpages.debian.org/jessie/libdate-manip-perl/Date::Manip::Examples.3pm.en.html https://manpages.debian.org/jessie/libdate-manip-perl/Date::...
- masklinn 8y agoI would expect most languages to have something like that available, TFA lists 3 different libraries for Javascript alone. The point here is not to talk about an innovation, but about the eventuality of an up-to-date, API built into browsers such that eventually it will not be necessary for each and every application to ship and keep updating such a formatting subsystem themselves.
- tyingq 8y agoI didn't share it to diminish anything. These libraries that attempt to parse free-form phrases are interesting to compare.
- ape4 8y agoI'd rather see a constant symbol like rtf.format(3.14, Intl.RelativeTimeFormat.SECOND);
- Faaak 8y agoMe too. It seems way "cleaner" that way, not mixing semantics
- wvenable 8y agoI agree. This doesn't seem to be a very clean API. It's extremely "stringy".
- guachesuedehack 8y agoI agree.
- snek 8y agoHow come?
- moreira 8y agoQuick - is 'seconds' a valid value for the second parameter? You don't know; there's no easy way to know. If they're constants you can look at all constants available for Intl.RelativeTimeFormat, IDEs can do autocomplete, analysers can check if the value is valid and alert if not. That's just off the top of my head; I imagine there are even more reasons.
- felixfbecker 8y agoTypeScript can check and autocomplete valid values of strings too, but I agree with the other points.
- deleted 8y ago[deleted]
- Waterluvian 8y ago
- jypepin 8y agoIt's nice to see more libraries helping for this kind of things. But looking at the article it seems like the api needs you to pass the language and the unit (days, hours, etc). Does anyone know if there exists an option to not add one? For dynamic values this feature is pretty important. For example, when I post a tweet, I need to see "a few minutes ago" but look at the tweet later, "34563434 minutes ago" is not great. If the developers have to start mapping time values to units (60: 'hours', '24': 'days', etc) that doesn't make the library very useful in a lot of cases.
- wongarsu 8y agoFrom the proposal [1]: > It is the caller's responsibility to handle cut-off logic such as deciding between displaying "in 7 days" or "in 1 week". This API does not support relative dates involving compound units. e.g "in 5 days and 4 hours". Specifying the locale is optional, and you can specify a list that will be matched against the user agent's preferences. 1: https://github.com/tc39/proposal-intl-relative-time#api https://github.com/tc39/proposal-intl-relative-time#api
- masklinn 8y ago> It is the caller's responsibility to handle cut-off logic such as deciding between displaying "in 7 days" or "in 1 week". That seems like it'd make the API quite significantly less convenient: it becomes unusable on its own and pretty much has to be wrapped in a helper of some sort.
- laurent123456 8y agoWill that be part of other browsers too or is it a Chrome-only thing?
- snek 8y agoIt will be in all browsers.
- fiter 8y agoDoes anyone know why this kind of time formatting has caught on? It takes timezones out of the picture, but also results in large rounding and sometimes the need to consult a calendar. Just below this comment box there are many "1 hour ago" references that presumably refer to any time between 1 and 1.499 hours ago -- at least 20% error?
- dbbk 8y agoBecause it's easier for most people to understand? It's not rocket science. Besides, most implementations keep the absolute date format in the alt tooltip if you need it.
- fredley 8y agoIt depends on context. Sometimes an absolute datetime value is going to be more appropriate, but often relative times are more meaningful and easier to parse (e.g. in this comment thread). Although as others have mentioned, I'd appreciate the full datetime in the title attribute whenever this is used!
- hateful 8y agohttps://xkcd.com/927/ https://xkcd.com/927/
- lanius 8y agoI understand the value of relative times, but I wish websites that use these would show the original timestamps as well. That includes you, Hacker News!
- aeharding 8y agoA lot of sites use a `title` attribute to hover to see the absolute timestamp. I'm surprised HN doesn't do this.
- underbluewaters 8y agoThis is so great. I'm looking forward to not having to use bloated js libraries to address date formatting.
- anamexis 8y agoA bit off-topic, but it seemed perverse that the Spanish word for "quarter" is "trimestre". Turns out that month = mes in spanish, so it's a three-month period, etymologically.
- timb07 8y ago"Trimester" is a word commonly used in English in relation to the stages of pregnancy.
- matthberg 8y agoAlso in the academic world where there is a quarter year system used (3 trimesters and summer break).
- wnoise 8y agoAlso some college use it rather than quarter or semester systems.
- repsilat 8y agoMm, and it's good to know it's etymologically closer to "three months" than "third-length period". (TIL also that "semester" means "six months". I'm an idiot, I thought it was "half-length period". Like "semi" I guess.)
- batat 8y agoI'd rather want to see this (among other stuff) as a part of "ECMAScript standard library" than web browser API.