moment-timezone icon indicating copy to clipboard operation
moment-timezone copied to clipboard

Create moment in zone from time-only uses wrong date

Open markdahmke opened this issue 9 years ago • 5 comments

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

markdahmke avatar Feb 01 '16 04:02 markdahmke

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).

mattjohnsonpint avatar Feb 01 '16 22:02 mattjohnsonpint

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 ?

mattjohnsonpint avatar Feb 01 '16 23:02 mattjohnsonpint

This is the same issue as #119, and requires changes to moment core to fix.

timrwood avatar Mar 08 '16 21:03 timrwood

I think https://github.com/moment/moment-rfcs/pull/1 could help provide a solution to this.

timrwood avatar Mar 15 '16 18:03 timrwood

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.

lpgm avatar Apr 22 '20 12:04 lpgm