问题:10位时间戳转换为时间
解决:
其一:通过substring方法,将13位的时间戳最后三位数字截取
其二:将13位时间戳除以1000取整。
(本文选取其二)
//转换方法:
getData(n) {
n = n ? n : null;
let now = new Date(n * 1000), //时间戳为10位需*1000,时间戳为13位的话不需乘1000
y = now.getFullYear(),
m = now.getMonth() + 1,
d = now.getDate();
return y + "/" + (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0,
8);
},
console.log("getData(item.last_login_time): ",this.getData('1685429993'));
知识点复盘:
- 时间戳(time-stamp)是一个加密后形成的凭证文档,由三部分组成
1、需加时间戳的文件的摘要(digest)
2、DTS收到文件的日期和时间
3、DTS的数字签名- 时间戳的目的:在于通过一定的技术手段,对数据产生的时间进行验证,从而验证这段数据在产生后是否经过篡改
- 疑点:13位的时间戳和10时间戳分别是怎么来的:
- Java的date默认精度是毫秒,生成的时间戳是13位,而像c++或者php生成的时间戳默认精度是秒,所以生成的时间戳是10位的
扩展: 时间转换为时间戳
timeToTimestamp(time){
let timestamp = Date.parse(new Date(time).toString());
//timestamp = timestamp / 1000; //时间戳为13位需除1000,为10位则不必
console.log(time + "的时间戳为:" + timestamp);
return timestamp;
//2023-05-30 14:59:53的时间戳为:1685429993
}
说明: