Is the Javascript date object always one day off?

后端 未结 23 2358
既然无缘
既然无缘 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:09

    You can convert this date to UTC date by

    new Date(Date.UTC(Year, Month, Day, Hour, Minute, Second))
    

    And it is always recommended to use UTC (universal time zone) date instead of Date with local time, as by default dates are stored in Database with UTC. So, it is good practice to use and interpret dates in UTC format throughout entire project. For example,

    Date.getUTCYear(), getUTCMonth(), getUTCDay(), getUTCHours()
    

    So, using UTC dates solves all the problem related to timezone issues.

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

    The following worked for me -

        var doo = new Date("2011-09-24").format("m/d/yyyy");
    
    0 讨论(0)
  • 2020-11-22 02:12

    If you want to get hour 0 of some date in the local time zone, pass the individual date parts to the Date constructor.

    new Date(2011,08,24); // month value is 0 based, others are 1 based.
    
    0 讨论(0)
  • 2020-11-22 02:13

    There are several crazy things that happen with a JS DATE object that convert strings, for example consider the following date you provided

    Note: The following examples may or may not be ONE DAY OFF depending on YOUR timezone and current time.

    new Date("2011-09-24"); // Year-Month-Day
    // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
    

    However, if we rearrange the string format to Month-Day-Year...

    new Date("09-24-2011");
    => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
    

    Another strange one

    new Date("2011-09-24");
    // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF AS BEFORE.
    
    new Date("2011/09/24"); // change from "-" to "/".
    => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
    

    We could easily change hyphens in your date "2011-09-24" when making a new date

    new Date("2011-09-24".replace(/-/g, '\/')); // => "2011/09/24".
    => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
    

    What if we had a date string like "2011-09-24T00:00:00"

    new Date("2011-09-24T00:00:00");
    // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
    

    Now change hyphen to forward slash as before; what happens?

    new Date("2011/09/24T00:00:00");
    // => Invalid Date
    

    I typically have to manage the date format 2011-09-24T00:00:00 so this is what I do.

    new Date("2011-09-24T00:00:00".replace(/-/g, '\/').replace(/T.+/, ''));
    // => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
    

    UPDATE

    If you provide separate arguments to the Date constructor you can get other useful outputs as described below

    Note: arguments can be of type Number or String. I'll show examples with mixed values.

    Get the first month and day of a given year

    new Date(2011, 0); // Normal behavior as months in this case are zero based.
    => // Sat Jan 01 2011 00:00:00 GMT-0700 (MST)
    

    Get the last month and day of a year

    new Date((2011 + 1), 0, 0); // The second zero roles back one day into the previous month's last day.
    => // Sat Dec 31 2011 00:00:00 GMT-0700 (MST)
    

    Example of Number, String arguments. Note the month is March because zero based months again.

    new Date(2011, "02"); 
    => // Tue Mar 01 2011 00:00:00 GMT-0700 (MST)
    

    If we do the same thing but with a day of zero, we get something different.

    new Date(2011, "02", 0); // again the zero roles back from March to the last day of February.
    => // Mon Feb 28 2011 00:00:00 GMT-0700 (MST)
    

    Adding a day of zero to any year and month argument will get the last day of the previous month. If you continue with negative numbers you can continue rolling back another day

    new Date(2011, "02", -1);
    => // Sun Feb 27 2011 00:00:00 GMT-0700 (MST)
    
    0 讨论(0)
  • 2020-11-22 02:14

    To normalize the date and eliminate the unwanted offset (tested here : https://jsfiddle.net/7xp1xL5m/ ):

    var doo = new Date("2011-09-24");
    console.log(  new Date( doo.getTime() + Math.abs(doo.getTimezoneOffset()*60000) )  );
    // Output: Sat Sep 24 2011 00:00:00 GMT-0400 (Eastern Daylight Time)
    

    This also accomplishes the same and credit to @tpartee (tested here : https://jsfiddle.net/7xp1xL5m/1/ ):

    var doo = new Date("2011-09-24");
    console.log( new Date( doo.getTime() - doo.getTimezoneOffset() * -60000 )  );
    
    0 讨论(0)
  • 2020-11-22 02:14

    Your issue is specifically with time zone. Note part GMT-0400 - that is you're 4 hours behind GMT. If you add 4 hours to the displayed date/time, you'll get exactly midnight 2011/09/24. Use toUTCString() method instead to get GMT string:

    var doo = new Date("2011-09-24");
    console.log(doo.toUTCString());
    
    0 讨论(0)
提交回复
热议问题