mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-02-24 19:06:37 -05:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import 'web/lib/dayjs'
|
|
|
|
import dayjs from '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}`
|
|
}
|