* esm * wip * wip * wip * wip * wip * wip * subscription notice * wip * wip * wip * fix envs * fix: update docker build * fix * esm/types * delete dashboard :D * add patches to dockerfiles * update packages + catalogs + ts * wip * remove native libs * ts * improvements * fix redirects and fetching session * try fix favicon * fixes * fix * order and resize reportds within a dashboard * improvements * wip * added userjot to dashboard * fix * add op * wip * different cache key * improve date picker * fix table * event details loading * redo onboarding completely * fix login * fix * fix * extend session, billing and improve bars * fix * reduce price on 10M
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import { differenceInDays, differenceInHours, isSameDay } from 'date-fns';
|
|
import type { FormatStyleName } from 'javascript-time-ago';
|
|
import TimeAgo from 'javascript-time-ago';
|
|
import en from 'javascript-time-ago/locale/en';
|
|
|
|
export function dateDifferanceInDays(date1: Date, date2: Date) {
|
|
const diffTime = Math.abs(date2.getTime() - date1.getTime());
|
|
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
|
}
|
|
|
|
export function getLocale() {
|
|
if (typeof navigator === 'undefined') {
|
|
return 'en-US';
|
|
}
|
|
|
|
return navigator.language ?? 'en-US';
|
|
}
|
|
|
|
export function formatDate(date: Date) {
|
|
const day = date.getDate();
|
|
const month = new Intl.DateTimeFormat(getLocale(), { month: 'short' })
|
|
.format(date)
|
|
.replace('.', '')
|
|
.toLowerCase();
|
|
|
|
return `${day} ${month}`;
|
|
}
|
|
|
|
export function formatDateTime(date: Date) {
|
|
const datePart = formatDate(date);
|
|
const timePart = new Intl.DateTimeFormat(getLocale(), {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false,
|
|
}).format(date);
|
|
|
|
return `${datePart}, ${timePart}`;
|
|
}
|
|
|
|
export function formatTime(date: Date) {
|
|
return new Intl.DateTimeFormat(getLocale(), {
|
|
hour: 'numeric',
|
|
minute: 'numeric',
|
|
second: 'numeric',
|
|
}).format(date);
|
|
}
|
|
|
|
TimeAgo.addDefaultLocale(en);
|
|
const ta = new TimeAgo(getLocale());
|
|
|
|
export function timeAgo(date: Date, style?: FormatStyleName) {
|
|
return ta.format(new Date(date), style);
|
|
}
|
|
|
|
export function formatTimeAgoOrDateTime(date: Date) {
|
|
if (Math.abs(differenceInHours(date, new Date())) < 3) {
|
|
return timeAgo(date);
|
|
}
|
|
|
|
return isSameDay(date, new Date()) ? formatTime(date) : formatDateTime(date);
|
|
}
|
|
|
|
export function utc(date: string) {
|
|
if (date.match(/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3}$/)) {
|
|
return new Date(`${date}Z`);
|
|
}
|
|
return new Date(date).toISOString();
|
|
}
|