4 ms·
Is there any built in for human strings for durations and times? “5 days ago.” “3 hours and 33 minutes.” I’ve found this kind of thing to be the main reason
by Waterluvian 2y ago
Is there any built in for human strings for durations and times?
“5 days ago.”
“3 hours and 33 minutes.”
I’ve found this kind of thing to be the main reason I have to drag MomentJS along for the ride.
- ramesh31 2y ago>I’ve found this kind of thing to be the main reason I have to drag MomentJS along for the ride. Likewise. It feels long overdue to have template based formatting natively.
- simonw 2y agoI’ve written versions of that half a dozen times over my career. I recommend ditching MomentJS and pasting in your own ~15 line implementation. I’m confident ChatGPT or Claude will write you one with one-shot prompt that does exactly what you want. Here’s an attempt at that: https://chatgpt.com/share/9bfe1d60-34cc-46fc-8327-3474c4a7d6ba https://chatgpt.com/share/9bfe1d60-34cc-46fc-8327-3474c4a7d6...
- jayflux 2y agoYes! You’re looking for DurationFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... which will work with Temporal types
- Waterluvian 2y agoThis looks perfect! Just gotta wait until anything supports it. I’m hoping it’s progressing and there’s plans to adopt it.
- roblh 2y agoI sure hope so. Moment + Moment TZ = just shy of200kb gzipped. Learned that one the hard way the other day.
- jitl 2y agoIt’s already available in browsers as part of Intl, no need to wait for Temporal https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Waterluvian 2y agoOh perfect! Thanks. I was looking at some other MDN page that suggested basically 0% support.
- lxgr 2y agoPet peeve: I mostly hate these. Anything for maybe the last hour or I can see; "1 day and 5 hours ago" and making me hover over the string (and pray that there's an alt text available that I still can't copy-paste) is very annoying. I really wish, space permitting, web designers would start at least including the actual timestamp in the regular/non-alt text as well, at the very least for professional tools if not for social media sites.
- Waterluvian 2y agoagreed it can be misused so easily. One time it works is when your team is across many time zones, the data being listed spans many time zones, and what’s important is knowing the recent events from older ones. And yes, also showing the absolute UTC stamps too