Using Intl.DurationFormat for Localized Durations

Last year I had the opportunity to give a talk on the web platform’s Intl specification. This made me incredibly happy because in preparing for the presentation, I discovered so many cool features and capabilities of the spec that I had no idea existed. Almost a year ago, I wrote up a blog post on Intl.RelativeTimeFormat, talking about how the API was easy to use, but perhaps a bit difficult when dealing when determining the best values to use when formatting dynamic dates. Today, I’m going to turn my attention to a related spec, Intl.DurationFormat The Basics The DurationFormat API works like so: Given a locale (defaulting to the browser’s locale) Given a set of values representing... more →
Posted in: JavaScript

Using Intl.RelativeTimeFormat for Localized Relative Timings

I’ve been singing the praises of the web platform’s Intl object for years now, but it still continues to impress me. While I’ve seen it before, today I came across the RelativeTimeFormat API which looks absolutely fabulous. I played with it a bit and thought I’d share some tips. The Basics # The RelativeTimeFormat API works like so: Given a locale… Given a difference (in either a positive or negative value)… And given a unit of time, like ‘hour’ (or ‘hours’) Report the difference in the user’s desired language. So for example: 3 and day, "in 3 days" -1 and day, "one day ago" 18 and hour, "in 18 hours" Also,... more →
Posted in: JavaScript