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

When given a UTC date, a Twix iteration is one day shorter than I expect #74

Closed barneycarroll closed 9 years ago

barneycarroll commented 9 years ago

As seen here.

barneycarroll commented 9 years ago

User error! The confusion is over how UTC affects the moment. According to my confused understanding, by invoking utc, Moment would operate in 'UTC mode', but this isn't the case: the start moment is in UTC, but the end moment isn't. Because I'm in BST (UTC+1), I'm ending up with a range that is always one hour short of the number of days I expect. Fixed version here.

icambron commented 8 years ago

Yeah, this has come up before (e.g. #15). Twix doesn't know that the start Moment is in UTC mode; it just creates a new Moment from the end argument; in fact it does little more than call moment() and use that. So yeah, the only thing to do here there:

moment.utc(start).twix(moment.utc(end));

//or

moment(start).utc().twix(moment(end).utc());

In theory I could fix this by checking isUTC() on the start Moment and then constructing the end moment differently, but that seems more confusing than anything else. What I would accept a PR for is an option in the option hash that let's you do:

moment.utc(start).twix(end, {utc: true});

and if so just calls .utc() after constructing the end.