Is the Javascript date object always one day off?

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

提交回复
热议问题