时间与时间戳之间的转换

//一,时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳

1.var timeone = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000

  console.log(timeone);

2.var timetwo = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值

console.log(timetwo);

3.var timethird = new Date().getTime(); // 结果:1477808630404 ,通过原型方法直接获得当前时间的毫秒值,准确

console.log(timethird);

4.var timefour = Number(new Date()) ; //结果:1477808630404 ,将时间转化为一个number类型的数值,即时间戳

console.log(timefour);

//二,时间戳转时间
//我是把后台传来的时间戳转为时间格式
requestApi(prefixtenant+"/getRechargeRecord/"+this.resourceId,{
}).then(res=>{
	this.payrecordlist = res.data
	for(let i = 0;i<this.payrecordlist.length;i++){
        //直接用 new Date(时间戳) 格式转化获得当前时间
		this.payrecordlist[i].rechargeDate =new Date(this.payrecordlist[i].rechargeDate)
        //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式
		this.payrecordlist[i].rechargeDate = 						  						this.payrecordlist[i].rechargeDate.toLocaleDateString().replace(/\//g, "-") + 
        "" + this.payrecordlist[i].rechargeDate.toTimeString().substr(0, 8);
	}
	console.log(this.payrecordlist);
}).catch(rej=>{
	console.log(err);
})
//不过这样转换在某些浏览器上会出现不理想的效果,因为toLocaleDateString()方法是因浏览器而异的,比如 IE为2016年8月24日 22:26:19 格式 搜狗为Wednesday, August 24, 2016 22:39:42
function getdate() {
    var now = new Date(),
 	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);
}


function formatDuring(mss){
     var days = parseInt(mss / (1000 * 60 * 60 * 24));
     var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
     var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
     var seconds = (mss % (1000 * 60)) / 1000;
     return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
}

formatDuring(86400)
//"0 天 0 小时 1 分钟 "
formatDuring(86400000)
//"1 天 0 小时 0 分钟 "


//项目中的
this.productListData.lists = this.productListData.lists.map((item) => {
						item.showTime = this.dayjs.duration(this.dayjs(item.endTime, "yyyy-MM-dd HH:mm:ss")
							.diff(this.dayjs())).format('DD:HH:mm:ss').split(':')
						item.showTime = item.showTime.map((item) => {
							item = item.substr(1);
							return item
						})
						console.log("时", item);
						console.log("时间", item.showTime);
						return item
					})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值