54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
const MINUTE = 60
|
|
const HOUR = MINUTE * 60
|
|
const DAY = HOUR * 24
|
|
const WEEK = DAY * 7
|
|
|
|
export function ago(date: number | string | Date): string {
|
|
let ts: number
|
|
if (typeof date === 'string') {
|
|
ts = Number(new Date(date))
|
|
} else if (date instanceof Date) {
|
|
ts = Number(date)
|
|
} else {
|
|
ts = date
|
|
}
|
|
const diffSeconds = Math.floor((Date.now() - ts) / 1e3)
|
|
if (diffSeconds < MINUTE) {
|
|
return `${diffSeconds}s`
|
|
} else if (diffSeconds < HOUR) {
|
|
return `${Math.floor(diffSeconds / MINUTE)}m`
|
|
} else if (diffSeconds < DAY) {
|
|
return `${Math.floor(diffSeconds / HOUR)}h`
|
|
} else if (diffSeconds < WEEK) {
|
|
return `${Math.floor(diffSeconds / DAY)}d`
|
|
} else {
|
|
return new Date(ts).toLocaleDateString('en-us', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
})
|
|
}
|
|
}
|
|
|
|
export function niceDate(date: number | string | Date) {
|
|
const d = new Date(date)
|
|
return `${d.toLocaleDateString('en-us', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
})} at ${d.toLocaleTimeString(undefined, {
|
|
hour: 'numeric',
|
|
minute: '2-digit',
|
|
})}`
|
|
}
|
|
|
|
export function getAge(birthDate: Date): number {
|
|
var today = new Date()
|
|
var age = today.getFullYear() - birthDate.getFullYear()
|
|
var m = today.getMonth() - birthDate.getMonth()
|
|
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
|
|
age--
|
|
}
|
|
return age
|
|
}
|