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
|
|
|
|
2023-03-23 13:59:32 +01:00
|
|
|
import { t } from 'i18next';
|
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-03 16:19:20 +02:00
|
|
|
export const epochToDate = (epoch: number): Date => {
|
|
|
|
|
const date = new Date(0); // The 0 there is the key, which sets the date to the epoch
|
|
|
|
|
date.setUTCSeconds(epoch);
|
|
|
|
|
return date;
|
2023-01-07 16:46:07 +01:00
|
|
|
};
|
|
|
|
|
|
2024-05-03 16:19:20 +02:00
|
|
|
export const isTheSameDay = (first: Date, second: Date): boolean =>
|
|
|
|
|
first.getDate() === second.getDate() &&
|
|
|
|
|
first.getMonth() === second.getMonth() &&
|
|
|
|
|
first.getFullYear() === second.getFullYear();
|
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
|
|
|
|
|
* const today = new Date();
|
|
|
|
|
* const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24);
|
|
|
|
|
* const someDate = new Date(1337, 4, 20);
|
|
|
|
|
*
|
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-03 16:19:20 +02:00
|
|
|
export const getDateString = (date: Date | number, withTime: boolean = false) => {
|
|
|
|
|
const actualDate = date instanceof Date ? date : new Date(date);
|
2023-01-07 16:46:07 +01:00
|
|
|
|
|
|
|
|
const today = new Date();
|
2024-05-03 16:19:20 +02:00
|
|
|
const yesterday = new Date();
|
|
|
|
|
yesterday.setDate(today.getDate() - 1);
|
2023-01-07 16:46:07 +01:00
|
|
|
|
2024-05-03 16:19:20 +02:00
|
|
|
const wasUploadedToday = isTheSameDay(today, actualDate);
|
|
|
|
|
const wasUploadedYesterday = isTheSameDay(yesterday, actualDate);
|
2023-01-07 16:46:07 +01:00
|
|
|
|
|
|
|
|
const addTimeString = wasUploadedToday || wasUploadedYesterday;
|
2024-05-03 16:19:20 +02:00
|
|
|
const timeString = addTimeString ? timeFormatter.format(actualDate) : '';
|
2023-01-07 16:46:07 +01:00
|
|
|
|
|
|
|
|
if (wasUploadedToday) {
|
2024-05-03 16:19:20 +02:00
|
|
|
if (withTime) {
|
|
|
|
|
return t('global.date.label.today_at', { timeString });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t('global.date.label.today');
|
2023-01-07 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wasUploadedYesterday) {
|
2024-05-03 16:19:20 +02:00
|
|
|
if (withTime) {
|
|
|
|
|
return t('global.date.label.yesterday_at', { timeString });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t('global.date.label.yesterday');
|
2023-01-07 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-03 16:19:20 +02:00
|
|
|
return dateFormatter.format(actualDate);
|
2023-01-07 16:46:07 +01:00
|
|
|
};
|