forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type I18n} from '@lingui/core'
2import {msg} from '@lingui/macro'
3
4export function niceDate(
5 i18n: I18n,
6 date: number | string | Date,
7 dateStyle: 'short' | 'medium' | 'long' | 'full' | 'dot separated' = 'long',
8) {
9 const d = new Date(date)
10
11 if (dateStyle === 'dot separated') {
12 return i18n._(
13 msg({
14 context: 'date and time formatted like this: [time] 路 [date]',
15 message: `${i18n.date(d, {timeStyle: 'short'})} 路 ${i18n.date(d, {dateStyle: 'medium'})}`,
16 }),
17 )
18 }
19
20 return i18n.date(d, {
21 dateStyle,
22 timeStyle: 'short',
23 })
24}
25
26export function getAge(birthDate: Date): number {
27 var today = new Date()
28 var age = today.getFullYear() - birthDate.getFullYear()
29 var m = today.getMonth() - birthDate.getMonth()
30 if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
31 age--
32 }
33 return age
34}
35
36/**
37 * Get a Date object that is N years ago from now
38 * @param years number of years
39 * @returns Date object
40 */
41export function getDateAgo(years: number): Date {
42 const date = new Date()
43 date.setFullYear(date.getFullYear() - years)
44 return date
45}
46
47/**
48 * Compares two dates by year, month, and day only
49 */
50export function simpleAreDatesEqual(a: Date, b: Date): boolean {
51 return (
52 a.getFullYear() === b.getFullYear() &&
53 a.getMonth() === b.getMonth() &&
54 a.getDate() === b.getDate()
55 )
56}