2023-01-07 16:46:07 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2023-05-18 13:17:41 +02:00
|
|
|
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { Dayjs } from 'dayjs';
|
|
|
|
|
import dayjs from 'dayjs';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { t } from '@lingui/core/macro';
|
2023-01-07 16:46:07 +01:00
|
|
|
|
2024-05-03 15:57:38 +02:00
|
|
|
export const timeFormatter = new Intl.DateTimeFormat(navigator.language, { hour: '2-digit', minute: '2-digit' });
|
|
|
|
|
export const dateFormatter = new Intl.DateTimeFormat(navigator.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
});
|
|
|
|
|
export const dateTimeFormatter = new Intl.DateTimeFormat(navigator.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-21 21:27:31 +02:00
|
|
|
export const epochToDate = (epoch: number): Dayjs => dayjs.unix(epoch);
|
2023-01-07 16:46:07 +01:00
|
|
|
|
2024-05-21 21:27:31 +02:00
|
|
|
export const isSameDay = (first: Dayjs, second: Dayjs): boolean => first.isSame(second, 'day');
|
2023-01-07 16:46:07 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a string in localized format for the passed date.
|
|
|
|
|
*
|
|
|
|
|
* In case the date is from today or yesterday a special string will be returned including "Today"
|
2024-05-03 16:19:20 +02:00
|
|
|
* or "Yesterday".
|
|
|
|
|
* Optionally this special string can include the localized time of the passed date ("Today/Yesterday at HH:mm").
|
2023-01-07 16:46:07 +01:00
|
|
|
*
|
|
|
|
|
* @example
|
2024-05-21 21:27:31 +02:00
|
|
|
* const today = dayjs();
|
|
|
|
|
* const yesterday = today.subtract(1, 'day');
|
|
|
|
|
* const someDate = dayjs('1377-04-20');
|
2023-01-07 16:46:07 +01:00
|
|
|
*
|
2024-05-03 16:19:20 +02:00
|
|
|
* const todayAsString = getDateString(today); // => "Today"
|
|
|
|
|
* const yesterdayAsString = getDateString(yesterday, true) // => "Yesterday at 02:50 AM"
|
2023-01-07 16:46:07 +01:00
|
|
|
* const someDate = getDateString(someDate) // => "04/20/1337"
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param date
|
2024-05-03 16:19:20 +02:00
|
|
|
* @param withTime
|
2023-01-07 16:46:07 +01:00
|
|
|
*/
|
2024-05-21 21:27:31 +02:00
|
|
|
export const getDateString = (date: Dayjs | number, withTime: boolean = false) => {
|
|
|
|
|
const actualDate = date instanceof dayjs ? date : dayjs(date);
|
|
|
|
|
const timeString = timeFormatter.format(actualDate.toDate());
|
2023-01-07 16:46:07 +01:00
|
|
|
|
2024-05-21 21:27:31 +02:00
|
|
|
if (actualDate.isToday()) {
|
2024-05-03 16:19:20 +02:00
|
|
|
if (withTime) {
|
2026-01-10 19:45:02 +01:00
|
|
|
return t`Today at ${timeString}`;
|
2024-05-03 16:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
return t`Today`;
|
2023-01-07 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-21 21:27:31 +02:00
|
|
|
if (actualDate.isYesterday()) {
|
2024-05-03 16:19:20 +02:00
|
|
|
if (withTime) {
|
2026-01-10 19:45:02 +01:00
|
|
|
return t`Yesterday at ${timeString}`;
|
2024-05-03 16:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
return t`Yesterday`;
|
2023-01-07 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-21 21:27:31 +02:00
|
|
|
return dateFormatter.format(actualDate.toDate());
|
2023-01-07 16:46:07 +01:00
|
|
|
};
|