-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IntervalFormat #188
Comments
|
@fabalbon is interested in this feature. |
Yes! I'm interested in adding this feature. I agree with Zibi's idea of folding this into Also, I think that this same approach can be used to add support for ranges on other formatters. My biggest concern with this would be the implementation. For instance, if using ICU and CLDR, when a A second question that needs to be discussed is if we should use |
Example usage: let date1 = new Date(Date.UTC(2007, 0, 10, 10, 0, 0));
let date2 = new Date(Date.UTC(2007, 0, 10, 11, 0, 0));
let date3 = new Date(Date.UTC(2007, 0, 20, 10, 0, 0));
// > 'Wed, 10 Jan 2007 10:00:00 GMT'
// > 'Wed, 10 Jan 2007 11:00:00 GMT'
// > 'Sat, 20 Jan 2007 10:00:00 GMT'
let fmt1 = new Intl.DateTimeFormat("en", {
year: '2-digit',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
});
console.log(fmt1.format(date1));
console.log(fmt1.formatRange(date1, date2));
console.log(fmt1.formatRange(date1, date3));
// > '1/10/07, 10:00 AM'
// > '1/10/07, 10:00 – 11:00 AM'
// > '1/10/07, 10:00 AM – 1/20/07, 10:00 AM'
let fmt2 = new Intl.DateTimeFormat("en", {
year: 'numeric',
month: 'short',
day: 'numeric'
});
console.log(fmt2.format(date1));
console.log(fmt2.formatRange(date1, date2));
console.log(fmt2.formatRange(date1, date3));
// > 'Jan 10, 2007'
// > 'Jan 10, 2007'
// > 'Jan 10 – 20, 2007' In addition to
@zbraniecki, any thoughts? |
If that's a concern, we can lazy-load/create DateIntervalFormat when 'formatRange' method is called, I think assuming that formatRange is more rarely used than format. As for formatRange vs formatInterval, I don't have a strong opinion on that. Well, I'm slightly more inclined to go with formatRange, but either way is fine. |
Ok! I prepared an initial stage 0 proposal with a possible API and alternatives: Regarding In particular, what is missing is a |
Going back to one of the previous questions, should the output of On the current proposal, the output consists of an array of objects each of which contains a // '10:00 – 11:00 AM'
[
{ type: 'hour', value: '10' },
{ type: 'literal', value: ':' },
{ type: 'minute', value: '00' },
{ type: 'literal', value: ' – ' },
{ type: 'hour', value: '11' },
{ type: 'literal', value: ':' },
{ type: 'minute', value: '00' },
{ type: 'literal', value: ' ' },
{ type: 'dayPeriod', value: 'AM' }
] When formatting date intervals, there is no guarantee that the first date will be printed first on the formatted string, or that it will be first on the Therefore, users of this method have no easy way of using a different style depending on the date from which each token comes from (first or second), they only can use a different style based on the type of the token ( A possible solution to this could be to add an additional field to the return objects to indicate the source date. For example: // '10:00 – 11:00 AM'
[
{ type: 'hour', value: '10', source: 1 },
{ type: 'literal', value: ':', source: 1 },
{ type: 'minute', value: '00', source: 1 },
{ type: 'literal', value: ' – ', source: 1 },
{ type: 'hour', value: '11', source: 2 },
{ type: 'literal', value: ':', source: 2 },
{ type: 'minute', value: '00', source: 2 },
{ type: 'literal', value: ' ', source: 2 },
{ type: 'dayPeriod', value: 'AM', source: 2 }
] Date intervals commonly contain fields that are shared between the two dates (such as Open questions:
|
👍 in identifying the sources. Though, I wouldn't tag Does it make sense to consider nested parts? For example: // '10:00 – 11:00 AM'
[
{
type: "rangeStart",
value: [
{ type: "hour", value: "10" },
{ type: "literal", value: ":" },
{ type: "minute", value: "00" }
]
},
{ type: "literal", value: " – " },
{
type: "rangeEnd",
value: [
{ type: "hour", value: "11" },
{ type: "literal", value: ":" },
{ type: "minute", value: "00" }
]
},
{ type: "literal", value: " " },
{ type: "dayPeriod", value: "AM" }
]; |
I like this approach! I think it's more intuitive, and I agree that is better to clearly indicate if a token is shared or if it doesn't belong to any of the two dates. The only issue I see with this (although I don't know how relevant it is), is that because we are encoding the token's date on the structure of the returned value, instead of simply putting that information on a tag that can be ignored, we are making this method more convoluted to use when it's user is not interested in the token's date. I'm not sure, but maybe an alternative might be to keep this array flat, but making the For example: // '10:00 – 11:00 AM'
[
{ type: 'hour', value: '10', source: "startRange" },
{ type: 'literal', value: ':', source: "startRange" },
{ type: 'minute', value: '00', source: "startRange" },
{ type: 'literal', value: ' – ', source: "shared" },
{ type: 'hour', value: '11', source: "endRange" },
{ type: 'literal', value: ':', source: "endRange" },
{ type: 'minute', value: '00', source: "endRange" },
{ type: 'literal', value: ' ', source: "shared" },
{ type: 'dayPeriod', value: 'AM', source: "shared" }
] A second benefit of keeping the array flat is that it would be compatible with what |
The flat style seems better for me and it seems to be the one in the current proposed spec text, right? |
Yes! We discussed this issue at the 402 working group when we started working on this proposal, and the group's consensus at the time was to use the flat approach. That is what is proposed on the current spec text draft. |
Should we close this issue since we already have formatRange proposal reach stage 3? |
I have the "active proposal" tag. We can close when formatRange reaches Stage 4 and is merged. |
https://github.com/tc39/ecma402/blob/master/README.md does not have 'Interval format'. So, I guess it's not on the table, yet.
This is a place holder for IntervalFormat. I'll add more details later.
For now, briefly what this API would do is to format an interval with the minimum redundancy given two points in time and a "pattern" (out of a fixed list of patterns).
For instance, given 2016-04-05 and 2016-04-07 and "yMMMd", the formatted result can be "April 5 - 7, 2016". For "MMMd", it'd be 'Apr 5 - 7'.
For 2017-05-12 and 2017-07-04, the result can be "May 12 - July 4" for "MMMd" and "May 12 - Jul 4, 2017" for "yMMMMd"
BTW, CLDR has necessary data and ICU has an implementation.
The text was updated successfully, but these errors were encountered: