10位时间戳转换为时间 js

问题: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
	  }

说明: 


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值