Здравствуйте, не могу разобраться с ошибкой в js коде связана с датой
let students = [
{
Name: 'Иванова',
LastName: 'Елена',
Surname: 'Григорьевна',
Birthday: new Date(1999, 2, 5),
StartYear: 2018,
Faculty: 'Физика'
},
{
Name:'Наталья',
LastName:'Ивановна',
Surname:'Лысенко',
Birthday: new Date(2004, 3, 1),
StartYear: 2022,
Faculty:'Биология',
},
{
Name:'Артем',
LastName:'Андреевич',
Surname:'Мурзин',
Birthday: new Date(2005, 9, 22),
StartYear:2019,
Faculty:'История',
},
{
Name:'Николай',
LastName:'Николаевич',
Surname:'Наумов',
Birthday:new Date(2003, 6,22),
StartYear: 2023,
Faculty:'Физика',
},
{
Name:'Виктория',
LastName:'Олеговна',
Surname:'Максимова',
Birthday:new Date(2004, 4, 3),
StartYear:2020,
Faculty:'Химия',
},
]
function formatDate(date){
var dd = date.getDate();
if (dd < 10) dd = '0' + dd;
var mm = date.getMonth() + 1;
if(mm < 10 ) mm = '0' + mm;
var yy = date.getFullYear();
if(yy < 10) yy = '0' + yy;
return dd + '.' + mm + '.' + yy;
}
function $getNewStudentPerson(stuObj) {
const $person = document.createElement('tr');
const $FIO = document.createElement('td');
const $Bithday = document.createElement('td');
const $Start = document.createElement('td');
const $Faculty = document.createElement('td');
$FIO.textContent = stuObj.Surname + ' ' + stuObj.Name + ' ' + stuObj.LastName;
$Bithday.textContent = formatDate(stuObj.Birthday);
$Start.textContent = stuObj.StartYear;
$Faculty.textContent = stuObj.Faculty;
$person.append($FIO, $Bithday, $Start, $Faculty);
return $person;
}
function getSortStudents(column, dir, studentsArray) {
const studentsCopy = [...studentsArray];
return studentsCopy.sort(function (studentA, studentB) {
const valueA = studentA[column];
const valueB = studentB[column];
if (column === 'Birthday' || column === 'StartYear') {
return dir ? valueA - valueB : valueB - valueA;
}
if (column === 'Faculty') {
const facultyA = (valueA === undefined) ? '' : valueA.toLowerCase();
const facultyB = (valueB === undefined) ? '' : valueB.toLowerCase();
return dir ? facultyA.localeCompare(facultyB) : facultyB.localeCompare(facultyA);
}
if (typeof valueA === 'undefined' || valueA === null) {
return dir ? 1 : -1;
}
if (typeof valueB === 'undefined' || valueB === null) {
return dir ? -1 : 1;
}
return dir ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);
});
}
let column = 'fio';
let columnDir = true;
Object.defineProperty(Object.prototype, 'fio', {
get: function () {
return this.Surname + ' ' + this.Name + ' ' + this.LastName;
},
enumerable: true,
});
function applyFilters() {
let filteredStudents = [...students];
const nameFilter = (document.getElementById('name-filter')?.value) || '';
const lastNameFilter = (document.getElementById('lastname-filter')?.value) || '';
const facultyFilter = (document.getElementById('faculty-filter')?.value) || '';
if (nameFilter) {
filteredStudents = filteredStudents.filter(student => student.fio.toLowerCase().includes(nameFilter.toLowerCase()));
}
if (lastNameFilter) {
filteredStudents = filteredStudents.filter(student => student.fio.toLowerCase().includes(lastNameFilter.toLowerCase()));
}
if (facultyFilter) {
filteredStudents = filteredStudents.filter(student => student.Faculty.toLowerCase().includes(facultyFilter.toLowerCase()));
}
return filteredStudents;
}
function clearFilters() {
const nameFilterInput = document.getElementById('name-filter');
const lastNameFilterInput = document.getElementById('lastname-filter');
const facultyFilterInput = document.getElementById('faculty-filter');
if (nameFilterInput) {
nameFilterInput.value = '';
}
if (lastNameFilterInput) {
lastNameFilterInput.value = '';
}
if (facultyFilterInput) {
facultyFilterInput.value = '';
}
}
document.getElementById('name-filter').addEventListener('input', function () {
render(applyFilters());
});
document.getElementById('faculty-filter').addEventListener('input', function () {
render(applyFilters());
});
document.getElementById('start-year-filter').addEventListener('input', function () {
render(applyFilters());
});
document.getElementById('finish-year-filter').addEventListener('input', function () {
render(applyFilters());
});
function getFromLocalStorage() {
const storedData = localStorage.getItem('students');
return storedData ? JSON.parse(storedData) : [];
}
let information = getFromLocalStorage();
window.addEventListener('load', function() {
information = getFromLocalStorage();
render(information);
});
function render(arr = students) {
const copyArr = [...arr];
const $studentList = document.getElementById('student-list');
const $studentListTHAll = document.querySelectorAll('.table-student th');
$studentList.innerHTML = '';
for (const stuObj of copyArr) {
const $newPerson = $getNewStudentPerson(stuObj);
$studentList.appendChild($newPerson);
}
$studentListTHAll.forEach((element) => {
element.addEventListener('click', function () {
const column = this.dataset.column;
columnDir = !columnDir;
render(getSortStudents(column, columnDir, students));
});
});
}
document.getElementById('reset-filter').addEventListener('click', function () {
clearFilters();
render(students);
});
document.getElementById('form-add').addEventListener('submit', function (event) {
event.preventDefault();
let newListObj = {
Name: document.getElementById('name').value,
LastName: document.getElementById('lastname').value,
Surname: document.getElementById('surname').value,
Birthday: new Date(document.getElementById('birthday').value),
StartYear: document.getElementById('startYear').value,
Faculty: document.getElementById('faculty').value,
};
// Добавляем нового студента в массив
students.push(newListObj);
// Сохраняем обновленный массив в локальное хранилище
saveToLocalStorage(students);
// Перерисовываем таблицу
render(getSortStudents(column, columnDir, applyFilters()));
clearFilters();
});
// Функция для сохранения данных в локальное хранилище
function saveToLocalStorage(data) {
localStorage.setItem('students', JSON.stringify(data));
}
getFromLocalStorage(students);
//render(getFromLocalStorage());
// Получаем данные из локального хранилища и отрисовываем их
render(students);
вот консоль выдал ошибки
main.js:55 Uncaught TypeError: date.getDate is not a function
at formatDate (main.js:55:19)
at $getNewStudentPerson (main.js:78:28)
at render (main.js:200:28)
at main.js:188:5