29 lines
665 B
TypeScript
29 lines
665 B
TypeScript
export const parseTimeSince = (date: string) => {
|
|
const seconds = Math.floor(
|
|
(new Date().getTime() - new Date(date).getTime()) / 1000
|
|
)
|
|
|
|
let interval = seconds / 31536000
|
|
|
|
if (interval > 1) {
|
|
return Math.floor(interval) + ' years'
|
|
}
|
|
interval = seconds / 2592000
|
|
if (interval > 1) {
|
|
return Math.floor(interval) + ' months'
|
|
}
|
|
interval = seconds / 86400
|
|
if (interval > 1) {
|
|
return Math.floor(interval) + 'd'
|
|
}
|
|
interval = seconds / 3600
|
|
if (interval > 1) {
|
|
return Math.floor(interval) + 'h'
|
|
}
|
|
interval = seconds / 60
|
|
if (interval > 1) {
|
|
return Math.floor(interval) + 'm'
|
|
}
|
|
return Math.floor(seconds) + 's'
|
|
}
|