mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {type I18n} from '@lingui/core'
2
3export function niceDate(i18n: I18n, date: number | string | Date) {
4 const d = new Date(date)
5
6 return i18n.date(d, {
7 dateStyle: 'long',
8 timeStyle: 'short',
9 })
10}
11
12export function getAge(birthDate: Date): number {
13 var today = new Date()
14 var age = today.getFullYear() - birthDate.getFullYear()
15 var m = today.getMonth() - birthDate.getMonth()
16 if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
17 age--
18 }
19 return age
20}
21
22/**
23 * Get a Date object that is N years ago from now
24 * @param years number of years
25 * @returns Date object
26 */
27export function getDateAgo(years: number): Date {
28 const date = new Date()
29 date.setFullYear(date.getFullYear() - years)
30 return date
31}
32
33/**
34 * Compares two dates by year, month, and day only
35 */
36export function simpleAreDatesEqual(a: Date, b: Date): boolean {
37 return (
38 a.getFullYear() === b.getFullYear() &&
39 a.getMonth() === b.getMonth() &&
40 a.getDate() === b.getDate()
41 )
42}