今天在开发的过程中,遇到一个比较坑自己的问题。
将时间戳转换成正常日期的时候,总是会返回Invalid Date。
排查了好久,在想为什么是这个结果,在控制台里面测试都是ok的呀,于是乎,想到了自己再后端定义的时候,时间戳定义的是字符串格式的数据,啊啊啊,问题就在这里,
解决办法:
方法1:时间戳要定义成数字类型
方法2:自己将返回的时间戳转换成数组类型
时间戳转换成时间的函数:
/*** 时间戳转换成时间 2020-01-02 00:00:00*/export const changeTimeStamp = (timestamp) => {let time1 = new Date(Number(timestamp))let year = time1.getFullYear();let month = time1.getMonth() + 1;let day = time1.getDate();let hour = time1.getHours();let min = time1.getMinutes();let seconds = time1.getSeconds();month = month < 10 ? ('0' + month) : monthday = day < 10 ? ('0' + day) : dayhour = hour < 10 ? '0' + hour : hourmin = min < 10 ? '0' + min : minseconds = seconds < 10 ? '0' + seconds : secondsreturn `${year}-${month}-${day} ${hour}:${min}:${seconds}`}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号