解决:JS 手机端LocalDate转string后加了8小时

描述:
localDate格式的时间从后台传过来,经过new Date()后,在PC上看是正常的,但是在手机端导致时间在原来的基础上加了8小时。(考虑到项目的体积,不打算装momentJs插件)
解决:

/* 设定 localDate 字段为后台传过来的时间,
 格式为2020-01-01T01:01:03.743212 */
const newLocal = localDate.split('.')[0].replace(/\-/g, '/').replace('T', '')
const newDate = new Date(newLocal)

之后再由newDate经过Date的getFullYear()等方法拿到时间的年月日等信息拼接成字符串,这样得到的日期就是正确的啦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章