计算两个时间戳之间的天数,小时,分钟。

tech2025-06-05  10

项目需求:后台数据库传递回一个活动截止时间的字符串(13位),根据这个截止时间字符串计算出当前时间与截止时间之间的天数,小时。 效果如图: JS代码如下:

// 商品详情 getGoodsDetail() { let that = this; that.$api(this.baseURL, { id:this.cleckedId }).then(res => { if (res.code === 200) { that.goodsInfo = res.data; // 如果需要计算活动截止时间 if(that.goodsInfo.blockingTime){ that.goodsInfo.blockingTime = that.getDaysAndHours(that.goodsInfo.blockingTime); } } }); }, // 计算两个时间戳之间相差的天数 getDaysAndHours(blockingTime){ let dateNow = new Date(); // 获取当前时间 let timeDiff = blockingTime - dateNow.getTime(); // 时间差的毫秒数 // timeDiff = 时间戳差值 let days = Math.floor(timeDiff / (24 * 3600 * 1000)); // 计算出天数 let leavel1 = timeDiff % (24 * 3600 * 1000); // 计算天数后剩余的时间 let hours = Math.floor(leavel1 / (3600 * 1000)); // 计算天数后剩余的小时数 // let leavel2 = timeDiff % (3600 * 1000); // 计算剩余小时后剩余的毫秒数 // let minutes = Math.floor(leavel2 / (60 * 1000)); // 计算剩余的分钟数 return days+'天'+hours+'小时'; }
最新回复(0)