Is the Javascript date object always one day off?

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

    It means 2011-09-24 00:00:00 GMT, and since you're at GMT -4, it will be 20:00 the previous day.

    Personally, I get 2011-09-24 02:00:00, because I'm living at GMT +2.

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

    nevermind, didn't notice the GMT -0400, wich causes the date to be yesterday

    You could try to set a default "time" to be 12:00:00

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

    I faced some issue like this. But my issue was the off set while getting date from database.

    this is stroed in the database and it is in the UTC format.

    2019-03-29 19:00:00.0000000 +00:00

    So when i get from database and check date it is adding offset with it and send back to javascript.

    It is adding +05:00 because this is my server timezone. My client is on different time zone +07:00.

    2019-03-28T19:00:00+05:00 // this is what i get in javascript.

    So here is my solution what i do with this issue.

    var dates = price.deliveryDate.split(/-|T|:/);
    var expDate = new Date(dates[0], dates[1] - 1, dates[2], dates[3], dates[4]);
    var expirationDate = new Date(expDate);
    

    So when date come from the server and have server offset so i split date and remove server offset and then convert to date. It resolves my issue.

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

    This probably is not a good answer, but i just want to share my experience with this issue.

    My app is globally use utc date with the format 'YYYY-MM-DD', while the datepicker plugin i use only accept js date, it's hard for me to consider both utc and js. So when i want to pass a 'YYYY-MM-DD' formatted date to my datepicker, i first convert it to 'MM/DD/YYYY' format using moment.js or whatever you like, and the date shows on datepicker is now correct. For your example

    var d = new Date('2011-09-24'); // d will be 'Fri Sep 23 2011 20:00:00 GMT-0400 (EDT)' for my lacale
    var d1 = new Date('09/24/2011'); // d1 will be 'Sat Sep 24 2011 00:00:00 GMT-0400 (EDT)' for my lacale
    

    Apparently d1 is what i want. Hope this would be helpful for some people.

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

    I encountered this exact problem where my client was on Atlantic Standard Time. The date value the client retrieved was "2018-11-23" and when the code passed it into new Date("2018-11-23") the output for the client was for the previous day. I created a utility function as shown in the snippet that normalized the date, giving the client the expected date.

    date.setMinutes(date.getMinutes() + date.getTimezoneOffset());

    var normalizeDate = function(date) {
      date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
      return date;
    };
    
    var date = new Date("2018-11-23");
    
    document.getElementById("default").textContent = date;
    document.getElementById("normalized").textContent = normalizeDate(date);
    <h2>Calling new Date("2018-11-23")</h2>
    <div>
      <label><b>Default</b> : </label>
      <span id="default"></span>
    </div>
    <hr>
    <div>
      <label><b>Normalized</b> : </label>
      <span id="normalized"></span>
    </div>

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

    Storing yyyy-mm-dd in MySql Date format you must do the following:

    const newDate = new Date( yourDate.getTime() + Math.abs(yourDate.getTimezoneOffset()*60000) );
    console.log(newDate.toJSON().slice(0, 10)); // yyyy-mm-dd
    
    0 讨论(0)
提交回复
热议问题