描述:
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()
等方法拿到时间的年月日等信息拼接成字符串,这样得到的日期就是正确的啦。