Files
Compass/web/lib/util/time.ts
MartinBraquet 46082c5f64 Add events page
2026-02-18 18:34:18 +01:00

65 lines
2.0 KiB
TypeScript

import dayjs from 'dayjs'
import 'web/lib/dayjs'
export function fromNow(time: number | string | Date, privacy: boolean = false, t: any = undefined, locale: string | null = null) {
let date = dayjs(time)
if (locale) date = date.locale(locale)
if (privacy && dayjs().diff(date, 'hour') < 24) {
const defaultPastDay = 'in the past day';
return t ? t('common.from-now.past_day', defaultPastDay) : defaultPastDay
}
return date.fromNow();
}
const FORMATTER = new Intl.DateTimeFormat('default', {
dateStyle: 'medium',
timeStyle: 'medium',
})
export const formatTime = FORMATTER.format
export function formatTimeShort(time: number | string | Date, locale: string | null = null, hourOnly: boolean | null = null) {
let date = dayjs(time)
let template = hourOnly ? 'h:mm A' : 'MMM D, h:mm A'
if (locale) {
date = date.locale(locale)
if (locale !== 'en') template = hourOnly ? 'HH:mm' : 'D MMM, HH:mm'
}
return date.format(template)
}
export function formatJustTime(time: number) {
return dayjs(time).format('h:mma')
}
export const getCountdownString = (endDate: Date) => {
const remainingTimeMs = endDate.getTime() - Date.now()
const isPast = remainingTimeMs < 0
const seconds = Math.floor(Math.abs(remainingTimeMs) / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
const days = Math.floor(hours / 24)
const hoursStr = `${hours % 24}h`
const minutesStr = `${minutes % 60}m`
const daysStr = `${days}d`
return `${isPast ? '-' : ''}${daysStr} ${hoursStr} ${minutesStr}`
}
export const getCountdownStringHoursMinutes = (endDate: Date) => {
const remainingTimeMs = endDate.getTime() - Date.now()
const isPast = remainingTimeMs < 0
const seconds = Math.floor(Math.abs(remainingTimeMs) / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
const hoursStr = `${hours % 24}h`
const minutesStr = `${minutes % 60}m`
return `${isPast ? '-' : ''} ${hoursStr} ${minutesStr}`
}