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

Daylight saving and add function #29

Closed
ShijunK opened this issue Aug 20, 2013 · 2 comments
Closed

Daylight saving and add function #29

ShijunK opened this issue Aug 20, 2013 · 2 comments

Comments

@ShijunK
Copy link

ShijunK commented Aug 20, 2013

it seems that when hitting daylight saving, a moment object with time zone can not compute add days properly

var day = moment("2012-11-04");
console.log(day);
console.log(day.add("days",1));

//output with firefox+firebug
//Sun Nov 04 2012 00:00:00 GMT-0400 { _i="2012-11-04", _f="YYYY-MM-DD ", _isUTC=false, more...}
//Mon Nov 05 2012 00:00:00 GMT-0500 { _i="2012-11-04", _f="YYYY-MM-DD ", _isUTC=false, more...}


var dayTZ = moment("2012-11-04").tz("America/New_York");
console.log(dayTZ);
console.log(dayTZ.add("days",1));

//Sun Nov 04 2012 00:00:00 GMT-0400 { _i="2012-11-04", _f="YYYY-MM-DD ", _isUTC=true, more...}
// instead Nov 05, it returns +1 hour
//Sun Nov 04 2012 01:00:00 GMT-0400 { _i="2012-11-04", _f="YYYY-MM-DD ", _isUTC=true, more...}
// and continue +24 hours after that
Mon Nov 05 2012 01:00:00 GMT-0500 { _i="2012-11-04", _f="YYYY-MM-DD ", _isUTC=true, more...}

var dayTZ2 = moment("2012-11-01").tz("America/New_York");
console.log(dayTZ2);
console.log(dayTZ2.add("days",1));

//when not crossing daylight saving, timezone works properly
Thu Nov 01 2012 00:00:00 GMT-0400 { _i="2012-11-01", _f="YYYY-MM-DD ", _isUTC=true, more...}
//Fri Nov 02 2012 00:00:00 GMT-0400 { _i="2012-11-01", _f="YYYY-MM-DD ", _isUTC=true, more...}

with timezone-data as:

define(["moment-timezone"], function (moment) {
    moment.tz.add({
        "zones": {
            "America/Los_Angeles": [
                "-7:52:58 - LMT 1883_10_18_12_7_2 -7:52:58",
                "-8 US P%sT 1946 -8",
                "-8 CA P%sT 1967 -8",
                "-8 US P%sT"
            ],
            "America/New_York": [
                "-4:56:2 - LMT 1883_10_18_12_3_58 -4:56:2",
                "-5 US E%sT 1920 -5",
                "-5 NYC E%sT 1942 -5",
                "-5 US E%sT 1946 -5",
                "-5 NYC E%sT 1967 -5",
                "-5 US E%sT"
            ]
        },
        "rules": {
            "US": [
                "1918 1919 2 0 8 2 0 1 D",
                "1918 1919 9 0 8 2 0 0 S",
                "1942 1942 1 9 7 2 0 1 W",
                "1945 1945 7 14 7 23 1 1 P",
                "1945 1945 8 30 7 2 0 0 S",
                "1967 2006 9 0 8 2 0 0 S",
                "1967 1973 3 0 8 2 0 1 D",
                "1974 1974 0 6 7 2 0 1 D",
                "1975 1975 1 23 7 2 0 1 D",
                "1976 1986 3 0 8 2 0 1 D",
                "1987 2006 3 1 0 2 0 1 D",
                "2007 9999 2 8 0 2 0 1 D",
                "2007 9999 10 1 0 2 0 0 S"
            ],
            "CA": [
                "1948 1948 2 14 7 2 0 1 D",
                "1949 1949 0 1 7 2 0 0 S",
                "1950 1966 3 0 8 2 0 1 D",
                "1950 1961 8 0 8 2 0 0 S",
                "1962 1966 9 0 8 2 0 0 S"
            ],
            "NYC": [
                "1920 1920 2 0 8 2 0 1 D",
                "1920 1920 9 0 8 2 0 0 S",
                "1921 1966 3 0 8 2 0 1 D",
                "1921 1954 8 0 8 2 0 0 S",
                "1955 1966 9 0 8 2 0 0 S"
            ]
        },
        "links": {}
    });
});
@zgmnkv
Copy link

zgmnkv commented Aug 21, 2013

Seems that it's a duplicate of the previous issue #28

@timrwood
Copy link
Member

This has been fixed with 0.1.0.

var day = moment("2012-11-04");
console.log(day.format());               // 2012-11-04T00:00:00-07:00
console.log(day.add("days",1).format()); // 2012-11-05T00:00:00-08:00

var dayTZ = moment("2012-11-04").tz("America/New_York");
console.log(dayTZ.format());               // 2012-11-04T02:00:00-05:00
console.log(dayTZ.add("days",1).format()); // 2012-11-05T02:00:00-05:00

Note also that moment("2012-11-04") is parsing into your local timezone, and then .tz("America/New_York") is changing to the America/New_York timezone. To parse into America/New_York, use moment.tz().

var dayTZ = moment.tz("2012-11-04", "America/New_York");
console.log(dayTZ.format());               // 2012-11-04T00:00:00-04:00
console.log(dayTZ.add("days",1).format()); // 2012-11-05T00:00:00-05:00

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

No branches or pull requests

3 participants