tc39 / proposal-intl-relative-time

`Intl.RelativeTimeFormat` specification [draft]
http://tc39.github.io/proposal-intl-relative-time/
215 stars 24 forks source link

Handle cut-off logic / distance with a given Date #105

Closed damianobarbati closed 5 years ago

damianobarbati commented 5 years ago

I was attempting to move from momentjs and similia but it's pretty hard since all the logic is given to the developer.

Cit:

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".

Could something like the ever make its way to the spec?

const rtf = new Intl.RelativeTimeFormat("en-US"); // any BCP 47
const postedAt = new Date('2019-02-20T13:00:00-06:00'); 
rtf.formatDiff(postedAt); //tomorrow 1:00 PM

//

const rtf = new Intl.RelativeTimeFormat("en-GB"); // any BCP 47
const postedAt = new Date('2019-02-20T13:00:00+02:00'); 
rtf.formatDiff(postedAt); //tomorrow 13:00

//

const rtf = new Intl.RelativeTimeFormat("en-GB"); // any BCP 47
const postedAt = new Date('2019-02-20T16:00:00+02:00'); 
rtf.formatDiff(postedAt); //in 2 hours
rxaviers commented 5 years ago

Hi! In the initial designs, we considered taking a Date object instead of a numeric value and use bestFit style to return something like what you show here, but these challenges prevented us to move into that direction.

damianobarbati commented 5 years ago

@rxaviers thanks for the clarification! Thus the Intl.RelativeTimeFormat is currently primarily intended as support for libraries rather then for direct use in cases like mine.

rxaviers commented 5 years ago

Correct. Libraries should fill in the higher level abstractions.

zbraniecki commented 5 years ago

@damianobarbati - Rafael also wrote a small library that can be used on top of the core API to handle discrete units - https://github.com/rxaviers/relative-time/

We use it in Firefox and it works great!

littledan commented 5 years ago

OK, seems like this question has been answered.