All files / helpers date.ts

100% Statements 13/13
100% Branches 10/10
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x   1x 12x     1x 3x           1x 5x 5x   5x 2x   3x 1x     2x            
const RUSSIAN_LOCALE = 'ru-RU';
 
export function isSameDay(a: Date, b: Date): boolean {
    return a.getDate() === b.getDate() && a.getMonth() === b.getMonth() && a.getFullYear() === b.getFullYear();
}
 
export function formatTime(date: Date): string {
    return date.toLocaleTimeString(RUSSIAN_LOCALE, {
        hour: '2-digit',
        minute: '2-digit',
    });
}
 
export function formatDateHeader(date: Date, referenceDate = new Date()): string {
    const yesterday = new Date(referenceDate);
    yesterday.setDate(yesterday.getDate() - 1);
 
    if (isSameDay(date, referenceDate)) {
        return 'Сегодня';
    }
    if (isSameDay(date, yesterday)) {
        return 'Вчера';
    }
 
    return date.toLocaleDateString(RUSSIAN_LOCALE, {
        day: 'numeric',
        month: 'long',
        year: date.getFullYear() !== referenceDate.getFullYear() ? 'numeric' : undefined,
    });
}