add long version of ago
This commit is contained in:
+12
-7
@@ -4,7 +4,7 @@ const HOUR = MINUTE * 60
|
||||
const DAY = HOUR * 24
|
||||
const MONTH_30 = DAY * 30
|
||||
const MONTH = DAY * 30.41675 // This results in 365.001 days in a year, which is close enough for nearly all cases
|
||||
export function ago(date: number | string | Date): string {
|
||||
export function ago(date: number | string | Date, long = false): string {
|
||||
let ts: number
|
||||
if (typeof date === 'string') {
|
||||
ts = Number(new Date(date))
|
||||
@@ -17,13 +17,13 @@ export function ago(date: number | string | Date): string {
|
||||
if (diffSeconds < NOW) {
|
||||
return `now`
|
||||
} else if (diffSeconds < MINUTE) {
|
||||
return `${diffSeconds}s`
|
||||
return `${diffSeconds}${long ? ' seconds' : 's'}`
|
||||
} else if (diffSeconds < HOUR) {
|
||||
return `${Math.floor(diffSeconds / MINUTE)}m`
|
||||
return `${Math.floor(diffSeconds / MINUTE)}${long ? ' minutes' : 'm'}`
|
||||
} else if (diffSeconds < DAY) {
|
||||
return `${Math.floor(diffSeconds / HOUR)}h`
|
||||
return `${Math.floor(diffSeconds / HOUR)}${long ? ' hours' : 'h'}`
|
||||
} else if (diffSeconds < MONTH_30) {
|
||||
return `${Math.round(diffSeconds / DAY)}d`
|
||||
return `${Math.round(diffSeconds / DAY)}${long ? ' days' : 'd'}`
|
||||
} else {
|
||||
let months = diffSeconds / MONTH
|
||||
if (months % 1 >= 0.9) {
|
||||
@@ -33,9 +33,14 @@ export function ago(date: number | string | Date): string {
|
||||
}
|
||||
|
||||
if (months < 12) {
|
||||
return `${months}mo`
|
||||
return `${months}${long ? ' months' : 'mo'}`
|
||||
} else {
|
||||
return new Date(ts).toLocaleDateString()
|
||||
const str = new Date(ts).toLocaleDateString()
|
||||
|
||||
if (long) {
|
||||
return `on ${str}`
|
||||
}
|
||||
return str
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user