做的一个投票系统, 记录投票已经开始多长时间了, 需要从数据库取出开始时间,与当前时间相比较,获取到时间差
import moment from "moment";
const duration = (start) => {
let startTime = moment(start).valueOf();
let endTime = moment().valueOf();
let usedTime = endTime - startTime;
let days = Math.floor(usedTime / (24 * 3600 * 1000));
let leavel = usedTime % (24 * 3600 * 1000);
let hours = Math.floor(leavel / (3600 * 1000));
let leavel2 = leavel % (3600 * 1000);
let minutes = Math.floor(leavel2 / (60 * 1000));
let leavel3 = leavel2 % (60 * 1000)
let seconds = Math.floor(leavel3 / 1000 )
return {"days":days, "hours":hours, "minutes":minutes, "seconds":seconds}
}
duration(startDate)
days: 0
hours: 0
minutes: 0
seconds: 13
}
|