moment-timezone
moment-timezone copied to clipboard
Create moment in zone from time-only uses wrong date
In node.js running on Amazon Lambda: where UTC date is "2016-02-01" var date= moment.tz('03:00','hh:mm', 'America/Chicago'); returns 2016-02-01T03:00:00-06:00 instead of =2016-01-31T03:00:00-06:00
Thanks
Confirmed. Thanks for the bug report, we'll fix this asap.
Also, note you should probably be using HH:mm
instead of hh:mm
. (though not the cause of this issue).
Looks like the problem is here:
function tz (input) {
var args = Array.prototype.slice.call(arguments, 0, -1),
name = arguments[arguments.length - 1],
zone = getZone(name),
out = moment.utc.apply(null, args); // <--------- here
if (zone && !moment.isMoment(input) && needsOffset(out)) {
out.add(zone.parse(out), 'minutes');
}
out.tz(name);
return out;
}
Because the args
passed to moment.utc
only include time and not date, the value is created as the current UTC date, plus the time provided. Later the time is adjusted for offset of the specified zone, but that doesn't correct the date being put on the wrong day.
In other words, it fails whenever the current UTC date is not the current date in the specified time zone.
Eluding me on how to fix at the moment. I'd prefer a solution that gets the original value correctly ahead of time, but all I can come up with is an approach for correcting after-the-fact.
Ideas @timrwood ?
This is the same issue as #119, and requires changes to moment core to fix.
I think https://github.com/moment/moment-rfcs/pull/1 could help provide a solution to this.
Is this still a known problem? I've been getting the wrong day when creating a moment with just a time. I've done a workaround - written a function that takes in the correct day and the desired time and returns a moment.