購(gòu)物項(xiàng)目一般有的商品會(huì)有限時(shí)搶購(gòu)商品织狐,限時(shí)搶購(gòu)又根據(jù)當(dāng)前時(shí)間來(lái)算有兩個(gè)狀態(tài):1.預(yù)售(當(dāng)前時(shí)間到開售時(shí)間之間的狀態(tài)) 2.在售(當(dāng)前時(shí)間到搶購(gòu)結(jié)束時(shí)間之間的狀態(tài))筏勒。
下面來(lái)介紹下之間的換算邏輯:
1.獲取當(dāng)前時(shí)間戳
// 獲取當(dāng)前時(shí)間戳
const currentTimestamp = new Date().getTime();
2.服務(wù)器返回的時(shí)間戳
// 獲取某個(gè)時(shí)間格式的時(shí)間戳
const otherTime = 服務(wù)端返回的時(shí)間;
/* eslint-disable */
const newstr = otherTime(/-/g,'/');
/* eslint-enable */
const date = new Date(newstr);
const otherTimestamp = date.getTime().toString();
3.計(jì)算兩者差值管行,獲取時(shí)間段
//在售(當(dāng)前時(shí)間到搶購(gòu)結(jié)束時(shí)間之間的狀態(tài))
if (parseFloat(currentTimestamp) >= parseFloat(otherTimestamp)) {
date3 = otherTimestamp - currentTimestamp;
// console.log('在售獲得差值:'+ date3);
}
//預(yù)售(當(dāng)前時(shí)間到開售時(shí)間之間的狀態(tài))
else if (parseFloat(currentTimestamp) < parseFloat(otherTimestamp)) {
date3 = otherTimestamp - currentTimestamp;
// console.log('預(yù)售獲得差值:'+ date3);
}
4.根據(jù)獲取的時(shí)間戳計(jì)算出天時(shí)分秒
// 天
const days = Math.floor(date3 / (24 * 3600 * 1000));
// 時(shí)
const leave1 = date3 % (24 * 3600 * 1000);
const hours = Math.floor(leave1 / (3600 * 1000));
// 分
const leave2 = leave1 % (3600 * 1000);
const minutes = Math.floor(leave2 / (60 * 1000));
// 秒
const leave3 = leave2 % (60 * 1000);
const seconds = Math.round(leave3 / 1000);
// console.log("天:"+days+",時(shí):"+hours+",分:"+minutes+",秒:"+seconds);