Files
openchamber/packages/ui/src/components/chat/message/timeFormat.ts
T

53 lines
1.7 KiB
TypeScript
Raw Normal View History

import { getCurrentIntlLocale } from '@/lib/i18n';
import { formatMessage, useI18nStore } from '@/lib/i18n/store';
2026-06-03 18:20:35 +03:00
import { formatTimeForPreference } from '@/lib/timeFormat';
import type { TimeFormatPreference } from '@/stores/useUIStore';
const isSameDay = (left: Date, right: Date): boolean => {
return (
left.getFullYear() === right.getFullYear() &&
left.getMonth() === right.getMonth() &&
left.getDate() === right.getDate()
);
};
const isYesterday = (date: Date, now: Date): boolean => {
const yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
return isSameDay(date, yesterday);
};
const isValidTimestamp = (timestamp: number): boolean => {
return Number.isFinite(timestamp) && !Number.isNaN(new Date(timestamp).getTime());
};
2026-06-03 18:20:35 +03:00
export const formatTimestampForDisplay = (timestamp: number, timeFormatPreference: TimeFormatPreference): string => {
if (!isValidTimestamp(timestamp)) {
return '';
}
const date = new Date(timestamp);
const now = new Date();
2026-06-03 18:20:35 +03:00
const timePart = formatTimeForPreference(date, timeFormatPreference);
const locale = getCurrentIntlLocale();
const dictionary = useI18nStore.getState().dictionary;
if (isSameDay(date, now)) {
return timePart;
}
if (isYesterday(date, now)) {
return formatMessage(dictionary, 'common.date.yesterdayWithTime', { time: timePart });
}
const monthPart = date.toLocaleString(locale, { month: 'short' });
const dayPart = date.getDate();
const datePart = `${monthPart} ${dayPart}`;
if (date.getFullYear() === now.getFullYear()) {
return `${datePart}, ${timePart}`;
}
return `${datePart}, ${date.getFullYear()}, ${timePart}`;
};