Javascript Date() give wrong date off by one hour

后端 未结 4 830
迷失自我
迷失自我 2020-12-03 21:33

I send this date from my controller in java (Spring-MVC) the type in mysql is datetime

@Temporal(TemporalType.TIMESTAM         


        
4条回答
  •  无人及你
    2020-12-03 22:12

    We should probably need more data about it, but it could be that nothing is wrong here, it depends how you set and get back your date.

    Basically 1443567600000 doesn't contains timezone. It represent Tue Sep 29 2015 23:00:00 from Greenwich. It's a moment in time that, of course, it different from any location that has a different timezone. The same moment, happens at different time (the midnight of GMT+1 is the 11pm of GMT).

    You have to store both the time and the timezone in your DB, and possibly send back to JS, otherwise it will be always interpreted differently depends by the local time of the client.

    To make an example:

    var d = new Date(2015, 8, 30);
    console.log(d.toJSON()); // In my case I got "2015-09-29T22:00:00.000Z"
    console.log(d.toDateString()); // "Wed Sep 30 2015"
    

提交回复
热议问题