Is the Javascript date object always one day off?

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

提交回复
热议问题