Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create moment in zone from time-only uses wrong date #297

Open
markdahmke opened this issue Feb 1, 2016 · 5 comments
Open

Create moment in zone from time-only uses wrong date #297

markdahmke opened this issue Feb 1, 2016 · 5 comments
Labels

Comments

@markdahmke
Copy link

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

@mattjohnsonpint
Copy link
Contributor

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 mattjohnsonpint changed the title possible problem when creating date from hh:mm Create moment in zone from time-only uses wrong date Feb 1, 2016
@mattjohnsonpint
Copy link
Contributor

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 ?

@timrwood
Copy link
Member

timrwood commented Mar 8, 2016

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

@timrwood
Copy link
Member

I think moment/moment-rfcs#1 could help provide a solution to this.

@lpgm
Copy link

lpgm commented Apr 22, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants