Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Incorrect "yesterday" examples #91

Closed
2 tasks
mathiasbynens opened this issue Oct 19, 2018 · 6 comments
Closed
2 tasks

Incorrect "yesterday" examples #91

mathiasbynens opened this issue Oct 19, 2018 · 6 comments

Comments

@mathiasbynens
Copy link
Member

The README contains several examples that result in "yesterday", e.g.

let rtf = new Intl.RelativeTimeFormat("en");

// Format relative time using the day unit.
rtf.format(-1, "day");
// > "yesterday"

(search for "yesterday" to find them all)

However, it seems like in the default case, "1 day ago" is returned instead of "yesterday" for this and other examples.

To get the result of "yesterday", the numeric: 'auto' setting is needed.

const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
rtf.format(-1, 'day');
// > 'yesterday'

This is verified by this particular Test262 test: https://github.com/tc39/test262/blob/a81cc4213b85461b6fbca1e50abd5cfb6b21ff54/implementation-contributed/v8/intl/relative-time-format/format-en.js#L120

There are two things to do here:

  • Document the numeric option in the README (currently, it's not mentioned at all)
  • Update the examples in the README
@littledan
Copy link
Member

Oops, thanks. PRs welcome!

@mathiasbynens
Copy link
Member Author

FWIW, I documented numeric: 'auto' as part of https://developers.google.com/web/updates/2018/10/intl-relativetimeformat. Feel free to quote that doc if it's helpful.

@romulocintra
Copy link
Member

Hi @littledan I'am preparing a PR based on Readme based on work done by @mathiasbynens , to have it aligned with code samples and documentation for MDN

@romulocintra
Copy link
Member

I'am not sure if its a behaviour or defect :

rtf.format( -0,"day"  );  //  0 days ago 
rtf.format( 0,"day"  );   //  in 0 days

@rxaviers
Copy link
Member

It's a behavior, but your question shows all developers will also be confused about it.

In practice, it's pointless to use numeric style for 0 values. You would want to see "Today" in your example instead, but in order to use "Today" correctly you need to take care of #14. Therefore, it's important to realize Intl.RelativeTimeFormat is a low level building block (at least at this point) that enables higher level abstractions such as moment.js and https://github.com/rxaviers/relative-time (used by PayPal, and by Mozilla).

@caiolima
Copy link
Contributor

Is there anything missing to close this issue after #98 landed?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants