forked from pancakeswap/pancake-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTimePeriods.ts
53 lines (44 loc) · 1.21 KB
/
getTimePeriods.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const MINUTE_IN_SECONDS = 60
const HOUR_IN_SECONDS = 3600
const DAY_IN_SECONDS = 86400
const MONTH_IN_SECONDS = 2629800
const YEAR_IN_SECONDS = 31557600
/**
* Format number of seconds into year, month, day, hour, minute, seconds
*
* @param seconds
*/
const getTimePeriods = (seconds: number) => {
let delta = seconds
const timeLeft = {
years: 0,
months: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
}
if (delta >= YEAR_IN_SECONDS) {
timeLeft.years = Math.floor(delta / YEAR_IN_SECONDS)
delta -= timeLeft.years * YEAR_IN_SECONDS
}
if (delta >= MONTH_IN_SECONDS) {
timeLeft.months = Math.floor(delta / MONTH_IN_SECONDS)
delta -= timeLeft.months * MONTH_IN_SECONDS
}
if (delta >= DAY_IN_SECONDS) {
timeLeft.days = Math.floor(delta / DAY_IN_SECONDS)
delta -= timeLeft.days * DAY_IN_SECONDS
}
if (delta >= HOUR_IN_SECONDS) {
timeLeft.hours = Math.floor(delta / HOUR_IN_SECONDS)
delta -= timeLeft.hours * HOUR_IN_SECONDS
}
if (delta >= MINUTE_IN_SECONDS) {
timeLeft.minutes = Math.floor(delta / MINUTE_IN_SECONDS)
delta -= timeLeft.minutes * MINUTE_IN_SECONDS
}
timeLeft.seconds = delta
return timeLeft
}
export default getTimePeriods