bsky-app/src/lib/strings/time.ts
Eric Bailey 8651f31ebb
Localize dates, counts (#5027)
* refactor: consistent localized formatting

* refactor: localized date time

* refactor: localize relative time with strings

* chore: fix typo from copy-paste

* Clean up useTimeAgo

* Remove old ago

* Const

* Reuse

* Prettier

---------

Co-authored-by: Mary <git@mary.my.id>
2024-08-29 19:22:53 -05:00

31 lines
746 B
TypeScript

import {I18n} from '@lingui/core'
export function niceDate(i18n: I18n, date: number | string | Date) {
const d = new Date(date)
return i18n.date(d, {
dateStyle: 'long',
timeStyle: 'short',
})
}
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
}
/**
* Compares two dates by year, month, and day only
*/
export function simpleAreDatesEqual(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
)
}