Здравствуйте, есть скрипт который подсчитывает дату с дня рождения.
но он неверно делает подсчеты.
день на один больше, время не +2 а +4 (для проверки выбиралась сегодняшняя дата)
вот сам скрипт
var yearLabel;
var monthLabel;
var dayLabel;
var hourLabel;
var minLabel;
var yearLabels = new Array(' год ', ' года', ' лет ');
var monthLabels = new Array(' месяц ', ' месяцы ', ' месяцев ');
var dayLabels = new Array(' день ', ' дня ', ' дней ');
var hourLabels = new Array(' час ', ' часы ', ' часов ');
var minLabels = new Array(' минута ', ' минуты ', ' минут ');
var yearGap = 1970;
function processRequest() {
// get elements to fill
var nameEl = document.getElementById('name');
var birthDateEl = document.getElementById('birthDate');
var timeFromBirthDateEl = document.getElementById('timeFromBirthDate');
//fill user name
nameEl.innerHTML += window.opener.lastName + ' ' +
window.opener.firstName + ' ' +
window.opener.patronymic;
//calculate time from birth date
var birthDate = new Date(window.opener.year, window.opener.month, window.opener.day);
var diff = new Date(new Date() - birthDate);
console.log()
fillLabels((diff.getFullYear() - yearGap), (diff.getMonth() + 1), diff.getDate(), diff.getHours(), diff.getMinutes());
//fill user's birth date
birthDateEl.innerHTML += birthDate.getFullYear() + '/' + (birthDate.getMonth() + 1) + '/' + birthDate.getDate();
//fill date difference
timeFromBirthDateEl.innerHTML += (diff.getFullYear() - yearGap) + yearLabel +
diff.getMonth() + monthLabel +
diff.getDate() + dayLabel +
diff.getHours() + hourLabel +
diff.getMinutes() + minLabel;
}
function fillLabels(yearDiff, monthDiff, dayDiff, hourDiff, minuteDiff) {
yearLabel = yearLabels[getSuffixIndex(yearDiff)];
monthLabel = monthLabels[getSuffixIndex(monthDiff)];
dayLabel = dayLabels[getSuffixIndex(dayDiff)];
hourLabel = hourLabels[getSuffixIndex(hourDiff)];
minLabel = minLabels[getSuffixIndex(minuteDiff)];
}
function getSuffixIndex(num) {
if (num == 1) {
return 0
}
else if (num >= 2 && num <= 4) {
return 1
}
else {
return 2
}
}
window.onload = processRequest;