2024-05-06 16:17:22 +02:00
|
|
|
import i18next from 'i18next';
|
|
|
|
|
|
2024-03-26 13:17:55 +01:00
|
|
|
export const getFormattedDate = (commentTimestamp: number) => {
|
|
|
|
|
if (commentTimestamp === undefined || isNaN(commentTimestamp)) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2024-05-06 16:17:22 +02:00
|
|
|
const locale = i18next.language || 'en';
|
2024-03-26 13:17:55 +01:00
|
|
|
const string = new Intl.DateTimeFormat(locale, {
|
|
|
|
|
hour12: false,
|
|
|
|
|
year: '2-digit',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
weekday: 'short',
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
second: '2-digit',
|
|
|
|
|
}).format(new Date(commentTimestamp * 1000));
|
|
|
|
|
if (locale.startsWith('ar')) {
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
const items = string.split(/,* /);
|
|
|
|
|
if (items.length === 3) {
|
|
|
|
|
const itemIsNumber = [items[0][0].match(/[0-9]/), items[1][0].match(/[0-9]/)];
|
|
|
|
|
if (itemIsNumber[0] && itemIsNumber[1]) {
|
|
|
|
|
return `${items[0]}(${items[2]})${items[1]}`;
|
|
|
|
|
}
|
|
|
|
|
if (itemIsNumber[0] && !itemIsNumber[1]) {
|
|
|
|
|
return `${items[0]}(${items[1]})${items[2]}`;
|
|
|
|
|
}
|
|
|
|
|
if (!itemIsNumber[0] && itemIsNumber[1]) {
|
|
|
|
|
return `${items[1]}(${items[0]})${items[2]}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string;
|
|
|
|
|
};
|