icambron/twix.js

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

barneycarroll opened this issue · 2 comments

As seen here.

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.

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.