Is the Javascript date object always one day off?

后端 未结 23 2322
既然无缘
既然无缘 2020-11-22 01:49

In my Java Script app I have the date stored in a format like so:

2011-09-24

Now when I try using the above value to create a new Date obje

相关标签:
23条回答
  • 2020-11-22 02:24

    Trying to add my 2 cents to this thread (elaborating on @paul-wintz answer).

    Seems to me that when Date constructor receives a string that matches first part of ISO 8601 format (date part) it does a precise date conversion in UTC time zone with 0 time. When that date is converted to local time a date shift may occur if midnight UTC is an earlier date in local time zone.

    new Date('2020-05-07')
    Wed May 06 2020 20:00:00 GMT-0400 (Eastern Daylight Time)
    

    If the date string is in any other "looser" format (uses "/" or date/month is not padded with zero) it creates the date in local time zone, thus no date shifting issue.

    new Date('2020/05/07')
    Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
    new Date('2020-5-07')
    Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
    new Date('2020-5-7')
    Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
    new Date('2020-05-7')
    Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
    

    So then one quick fix, as mentioned above, is to replace "-" with "/" in your ISO formatted Date only string.

    new Date('2020-05-07'.replace('-','/'))
    Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
    
    0 讨论(0)
  • 2020-11-22 02:26

    Just want to add that apparently adding a space at the end of the string will use UTC for creation.

    new Date("2016-07-06")
    > Tue Jul 05 2016 17:00:00 GMT-0700 (Pacific Daylight Time)
    
    new Date("2016-07-06 ")
    > Wed Jul 06 2016 00:00:00 GMT-0700 (Pacific Daylight Time)
    

    Edit: This is not a recommended solution, just an alternative answer. Please do not use this approach since it is very unclear what is happening. There are a number of ways someone could refactor this accidentally causing a bug.

    0 讨论(0)
  • 2020-11-22 02:26

    This through me for a loop, +1 on zzzBov's answer. Here is a full conversion of a date that worked for me using the UTC methods:

    //myMeeting.MeetingDate = '2015-01-30T00:00:00'
    
    var myDate = new Date(myMeeting.MeetingDate);
    //convert to JavaScript date format
    //returns date of 'Thu Jan 29 2015 19:00:00 GMT-0500 (Eastern Standard Time)' <-- One Day Off!
    
    myDate = new Date(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate());
    //returns date of 'Fri Jan 30 2015 00:00:00 GMT-0500 (Eastern Standard Time)' <-- Correct Date!
    
    0 讨论(0)
  • 2020-11-22 02:26

    You are using the ISO date string format which, according to this page, causes the date to be constructed using the UTC timezone:

    Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

    If you format the text differently, such as "Jan 01 1970", then (at least on my machine) it uses your local timezone.

    0 讨论(0)
  • 2020-11-22 02:27

    Notice that Eastern Daylight Time is -4 hours and that the hours on the date you're getting back are 20.

    20h + 4h = 24h
    

    which is midnight of 2011-09-24. The date was parsed in UTC (GMT) because you provided a date-only string without any time zone indicator. If you had given a date/time string w/o an indicator instead (new Date("2011-09-24T00:00:00")), it would have been parsed in your local timezone. (Historically there have been inconsistencies there, not least because the spec changed more than once, but modern browsers should be okay; or you can always include a timezone indicator.)

    You're getting the right date, you just never specified the correct time zone.

    If you need to access the date values, you can use getUTCDate() or any of the other getUTC*() functions:

    var d,
        days;
    d = new Date('2011-09-24');
    days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
    console.log(days[d.getUTCDay()]);
    
    0 讨论(0)
  • 2020-11-22 02:29

    I believe that it has to do with time-zone adjustment. The date you've created is in GMT and the default time is midnight, but your timezone is EDT, so it subtracts 4 hours. Try this to verify:

    var doo = new Date("2011-09-25 EDT");
    
    0 讨论(0)
提交回复
热议问题