function getFormatDate(date) {
const daysOfWeek = ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'];
const monthsOfYear = ['Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек'];
const currentDate = new Date();
const currentDateMs = currentDate.getTime();
const offset = currentDate.getTimezoneOffset() * 60 * 1000;
const receivedDateMs = new Date(date).getTime() - offset;
const receivedDate = new Date(receivedDateMs);
const receivedDateDay = receivedDate.getDate();
const currentDay = currentDate.getDate();
const dayDiff = currentDay - receivedDateDay;
const hours = (receivedDate.getHours() < 10) ? '0' + receivedDate.getHours() : receivedDate.getHours();
const minutes = (receivedDate.getMinutes() < 10) ? '0' + receivedDate.getMinutes() : receivedDate.getMinutes();
const pattern = `${hours}:${minutes}`;
if (dayDiff === 0) {
const diff = (currentDateMs - receivedDateMs);
if (diff < 3600000) {
if (diff < 60) {
return 'Только что';
}
return `${Math.round(diff / 1000 / 60)} мин. назад`;
}
return 'Сегодня в ' + pattern;
} else if (dayDiff === 1) {
return 'Вчера в ' + pattern;
} else if (dayDiff < 7) {
return `${daysOfWeek[new Date(date).getDay()]} в ` + pattern;
} else {
return `${receivedDate.getDate()} ${monthsOfYear[receivedDate.getDay()]} ${receivedDate.getFullYear()}`;
}
}
const allComments = [...document.querySelectorAll('.comment__date'), ...document.querySelectorAll('.comment__reply-date')];
for (let i = 0; i < allComments.length; i++) {
allComments[i].innerText = getFormatDate(allComments[i].innerText);
}