puzzle/common/Time.js

48 lines
834 B
JavaScript
Raw Normal View History

2021-04-14 19:30:45 +02:00
const MS = 1
const SEC = MS * 1000
const MIN = SEC * 60
const HOUR = MIN * 60
const DAY = HOUR * 24
export const timestamp = () => {
const d = new Date();
return Date.UTC(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds(),
d.getUTCMilliseconds(),
)
}
2021-05-13 22:45:55 +02:00
export const durationStr = (duration) => {
const d = Math.floor(duration / DAY)
duration = duration % DAY
2021-04-14 19:30:45 +02:00
2021-05-13 22:45:55 +02:00
const h = Math.floor(duration / HOUR)
duration = duration % HOUR
2021-04-14 19:30:45 +02:00
2021-05-13 22:45:55 +02:00
const m = Math.floor(duration / MIN)
duration = duration % MIN
2021-04-14 19:30:45 +02:00
2021-05-13 22:45:55 +02:00
const s = Math.floor(duration / SEC)
2021-04-14 19:30:45 +02:00
return `${d}d ${h}h ${m}m ${s}s`
}
2021-05-13 22:45:55 +02:00
export const timeDiffStr = (from, to) => durationStr(to - from)
2021-04-14 19:30:45 +02:00
export default {
MS,
SEC,
MIN,
HOUR,
DAY,
timestamp,
timeDiffStr,
2021-05-13 22:45:55 +02:00
durationStr,
2021-04-14 19:30:45 +02:00
}