//一,时间转时间戳: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
})
时间与时间戳之间的转换
最新推荐文章于 2024-04-29 17:01:02 发布