48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
export function timeSince (timeStamp) {
|
|
const now = new Date()
|
|
const secondsPast = (now.getTime() - timeStamp) / 1000
|
|
if (secondsPast < 60) {
|
|
return parseInt(secondsPast) + 's'
|
|
}
|
|
if (secondsPast < 3600) {
|
|
return parseInt(secondsPast / 60) + 'm'
|
|
}
|
|
if (secondsPast <= 86400) {
|
|
return parseInt(secondsPast / 3600) + 'h'
|
|
}
|
|
if (secondsPast > 86400) {
|
|
const day = timeStamp.getDate()
|
|
const month = timeStamp.toDateString().match(/ [a-zA-Z]*/)[0].replace(' ', '')
|
|
const year = timeStamp.getFullYear() === now.getFullYear() ? '' : ' ' + timeStamp.getFullYear()
|
|
return day + ' ' + month + year
|
|
}
|
|
|
|
return 'now'
|
|
}
|
|
|
|
export function dayPivot (date, days) {
|
|
return new Date(date.getTime() + days * 24 * 60 * 60 * 1000)
|
|
}
|
|
|
|
export function timeLeft (timeStamp) {
|
|
const now = new Date()
|
|
const secondsPast = (timeStamp - now.getTime()) / 1000
|
|
|
|
if (secondsPast < 0) {
|
|
return false
|
|
}
|
|
|
|
if (secondsPast < 60) {
|
|
return parseInt(secondsPast) + 's'
|
|
}
|
|
if (secondsPast < 3600) {
|
|
return parseInt(secondsPast / 60) + 'm'
|
|
}
|
|
if (secondsPast <= 86400) {
|
|
return parseInt(secondsPast / 3600) + 'h'
|
|
}
|
|
if (secondsPast > 86400) {
|
|
return parseInt(secondsPast / (3600 * 24)) + ' days'
|
|
}
|
|
}
|