icambron / twix.js

:hourglass::left_right_arrow: A date range plugin for moment.js
https://isaaccambron.com/twix.js/
MIT License
379 stars 54 forks source link

Identical moments return an empty string instead of a single day format #105

Closed kwilliams-curago closed 6 years ago

kwilliams-curago commented 6 years ago
moment.twix('05/25/2012', '05/25/2012', 'MM/DD/YYYY').format()
""
icambron commented 6 years ago

Those are the same moment in time, so there's no range. But maybe you mean this:

moment.twix('05/25/2012', '05/25/2012', 'MM/DD/YYYY', {allDay: true}).format() //=> "May 25, 2012"
kwilliams-curago commented 6 years ago

Hey, thanks for the prompt response. I understand, however in my opinion the default behavior should be to return the formatted time.

icambron commented 6 years ago

Remember that Twix doesn't really "know" you sent in two dates. It only knows you sent in two really specific times (May 25, 2012, at 00:00:00.000) and that those two times are the same. It has no sense that the appropriate resolution of formatting is the date.

Consider, what you expect this to return:

var now = moment();
moment.twix(now, now).format();

Surely not the current date, right?

kwilliams-curago commented 6 years ago

In our use case, it comes down to smart formatting.

Given that the inputs can spit out a valid date format I see this edge case as a bug.

If I am asking for a date format and the dates happen to share a timestamp, I definitely did not expect to receive an empty string.

As per your point about resolution, the library already makes assumptions:

moment().twix(moment().add(1, 'second')).format()
// "May 17, 3:11 PM"
icambron commented 6 years ago

If you were asking it to say "May 25, 1982, 12:00 AM" in this case, I'd see your point. But you're really asking it to hardcode knowledge of your specific use case of by deciding that two equal times should render in "single day format". The library not intuiting your intention is not a bug; in fact Twix has an option specifically for telling it that's what you mean.

kwilliams-curago commented 6 years ago

No worries, thanks for chatting. It's not a big deal if this isn't included in the library.

I'm not asking it to hard code anything, just deal with an edge case. I don't see returning an empty string in this scenario as the right behavior for a string formatter.

Even ISO 8601 allows for a zero length duration: https://stackoverflow.com/questions/29823129/what-is-the-canonical-representation-of-zero-in-iso-8601-durations (although the format is a little ambiguous)