1 import { dateLocale } from '@proton/shared/lib/i18n'
3 const MILLISECONDS_IN_DAY = 86400000
5 export class DateFormatter {
6 private relativeDayFormatter = new Intl.RelativeTimeFormat(dateLocale.code, { numeric: 'auto' })
7 private weekdayFormatter = new Intl.DateTimeFormat(dateLocale.code, { weekday: 'long' })
8 private dateFormatter = new Intl.DateTimeFormat(dateLocale.code, {
12 private timeFormatter = new Intl.DateTimeFormat(dateLocale.code, {
18 private getDifferenceInDays(date1: Date | number, date2: Date | number) {
19 const date1StartOfDay = new Date(date1).setHours(0, 0, 0, 0)
20 const date2StartOfDay = new Date(date2).setHours(0, 0, 0, 0)
22 return (date1StartOfDay - date2StartOfDay) / MILLISECONDS_IN_DAY
25 formatDate(date: Date | number) {
26 const differenceInDays = this.getDifferenceInDays(date, new Date())
27 const isYesterdayOrToday = differenceInDays > -2
28 const isWithinAWeek = differenceInDays > -6
30 if (isYesterdayOrToday) {
31 return this.relativeDayFormatter.format(differenceInDays, 'day')
35 return this.weekdayFormatter.format(date)
38 return this.dateFormatter.format(date)
41 formatTime(date: Date | number) {
42 return this.timeFormatter.format(date)