Здравствуйте, нужна дата для сообщение сделал код пишет: Вместо "Сегодня": NaN Invalid Date NaN , в NaN:NaN
Как исправить вот код:
$(document).ready(()=>{
check()
const socket = io()
const cred = JSON.parse(localStorage.getItem("userData"))
$.ajax({
url: "/getmessage",
type: "POST",
success: function(msg) {
if(msg.status == "err"){
new Notification('Ошибка', {body: msg.msg})
}else{
msg.data.forEach(el => {
// Форматирование времени
let msgDate = formatDate(el.time_mark)
let currentDate = formatDate(new Date())
let x = new Date(currentDate); // 1 Дата
let y = new Date(msgDate); // 2 Дата
if(Date.parse(x) == Date.parse(y)){
console.log("=")
let Strtime = "Сегодня, в "+`${new Date(el.time_mark).getHours()}:${new Date(el.time_mark).getMinutes()}`
} else {
let date = new Date(el.time_mark); // 2009-11-10
let month = date.toLocaleString('default', { month: 'long' });
console.log(new Date(el.time_mark))
let Strtime = `${new Date(el.time_mark).getDay()} ${month} ${new Date(el.time_mark).getFullYear()} , в ${new Date(el.time_mark).getHours()}:${new Date(el.time_mark).getMinutes()}`
}
$("#msg-container").append(`
<div class="msg-box" id='${el.id}'>
<p class="author">${el.nick}</p>
<p class="msg-text">${el.content}</p>
<span class="time">${Strtime}</span>
</div>
`);
if(el.nick != cred.nick){
$(`#${el.id}`).addClass("not-my")
}
});
$("#all_mess").scrollTop($("#msg-container").height())
}
}
});
socket.emit('hello', ({nick: cred.nick}))
$("#messForm").submit((e)=>{
e.preventDefault();
let msg = $("#message").val()
if(!msg){
}else{
socket.emit('sendMess', {token: localStorage.getItem('token'), msg: $("#message").val()});
$("#message").val('');
}
})
socket.on('addMess', function (data) {
// Форматирование времени
let msgDate = formatDate(data.time_mark)
let currentDate = formatDate(new Date())
let x = new Date(currentDate); // 1 Дата
let y = new Date(msgDate); // 2 Дата
if (Date.parse(x) == Date.parse(y)) {
console.log("=")
let Strtime = "Сегодня, в " + `${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
} else {
let date = new Date(data.time_mark); // 2009-11-10
console.log(date.time_mark)
let month = date.toLocaleString('default', { month: 'long' });
console.log(new Date(data.time_mark))
let Strtime = `${new Date(data.time_mark).getDay()} ${month} ${new Date(data.time_mark).getFullYear()} , в ${new Date(data.time_mark).getHours()}:${new Date(data.time_mark).getMinutes()}`
$("#msg-container").append(`
<div class="msg-box" id='${data.id}'>
<p class="author">${data.author}</p>
<p class="msg-text">${data.msg}</p>
<span class="time">${Strtime}</span>
</div>
`);
if (data.author != cred.nick) {
$(`#${data.id}`).addClass("not-my")
}
$("#all_mess").scrollTop($("#msg-container").height())
}
});
socket.on('toLogin', function(data){
window.location.href = '/auth'
})
socket.on('helloMess', (str)=>{
$("#msg-container").append(`<span class="hello">${str.mess}</span>`)
$("#all_mess").scrollTop($("#msg-container").height())
//setInterval(hideEl, 3000)
})
});
function hideEl(){
$('.hello').hide()
}
function formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}