-
Notifications
You must be signed in to change notification settings - Fork 24
add support for a custom value of now
when calling format()
#4
Comments
What's the use case please @caridy? |
@ericf might have more information here, but if I remember correctly, there are to main use-cases here:
I will say that this is not a primary use-case, and we have learned to mitigate them using other tricks, but this is the first formatting process that takes into consideration a variable environment value (now). |
Thanks. Interestingly, #6 has the same root cause. Can we use a generic solution that works for both? Both are caused due to the lack of control over the relative time calculation performed by the implementation, e.g., Any ideas? |
There's something I like about let rf = Intl.RelativeTimeFormat('en');
let now = Date.now();
console.log(rf.format(date - now)); // "10 seconds ago" The main issue I see with this is people getting the values switched around when subtracting. |
Can the formatter be configured (by an option) to either accept the date instance or delta? let rf = Intl.RelativeTimeFormat('en'); // defaults to {inputType: 'date'}
console.log(rf.format(date)); // "10 seconds ago"
let rf = Intl.RelativeTimeFormat('en', {inputType: 'millisecond'});
let now = Date.now();
console.log(rf.format(date - now)); // "10 seconds ago"
let rf = Intl.RelativeTimeFormat('en', {inputType: 'second'});
console.log(rf.format(-10); // "10 seconds ago" |
@ericf, your example looks nice, although I'm afraid we can't use it considering the formatter also assumes a number is an epoch number, in which case it applies |
Approach 1, consider numbers as Epoch numbers. (I understand this is the existing proposal) let rf = Intl.RelativeTimeFormat('en');
let now = Date.now();
console.log(rf.format(date)); // "10 seconds ago"
console.log(rf.format(date.getTime())); // "10 seconds ago"
console.log(rf.format(now)); // "now"
console.log(rf.format(new Date(now))); // "now"
console.log(rf.format(date - now)); // "<bazillion> seconds ago" !! Approach 2, consider numbers as relative distances in ms. let rf = Intl.RelativeTimeFormat('en');
let now = Date.now();
console.log(rf.format(date)); // "10 seconds ago"
console.log(rf.format(date.getTime())); // "in <bazillion> seconds" !!
console.log(rf.format(now)); // "in <bazillion> seconds" !!
console.log(rf.format(new Date(now))); // "now"
console.log(rf.format(date - now)); // "10 seconds ago" Approach 3, formatter function to accept let rf = Intl.RelativeTimeFormat('en');
console.log(rf.format(date, {now: Date.now()})); // "10 seconds ago" |
There are a couple of things to consider:
I'm leaning toward the relative distance in ms as the most simple case, but I will like to spend more time on this because we make a decision. |
No description provided.
The text was updated successfully, but these errors were encountered: