-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeLeft.js
36 lines (35 loc) · 960 Bytes
/
timeLeft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @desc ${startTime - endTime}的剩余时间,startTime大于endTime时,均返回0
* @param { Date | String } startTime
* @param { Date | String } endTime
* @returns { Object } { d, h, m, s } 天 时 分 秒
*/
const timeLeft = (startTime, endTime) => {
if (!startTime || !endTime) {
return
}
var startDate, endDate
if (startTime instanceof Date) {
startDate = startTime
} else {
startDate = new Date(startTime.replace(/-/g, '/')) //开始时间
}
if (endTime instanceof Date) {
endDate = endTime
} else {
endDate = new Date(endTime.replace(/-/g, '/')) //结束时间
}
var t = endDate.getTime() - startDate.getTime()
var d = 0,
h = 0,
m = 0,
s = 0
if (t >= 0) {
d = Math.floor(t / 1000 / 3600 / 24)
h = Math.floor((t / 1000 / 60 / 60) % 24)
m = Math.floor((t / 1000 / 60) % 60)
s = Math.floor((t / 1000) % 60)
}
return { d, h, m, s }
}
module.exports = timeLeft