I am just writing a program to calculate the passage of time from the day of birth and the time left until my next birthday (JavaScript). I do not know how to take into account the time zones and leap years, which is why errors in calculations come to me - days and hours do not match.
var year; //variable that stores the user's birthday year
var month; //variable that stores the user's birthday month
var day; //variable that stores the user's birthday day
var todayDate = new Date();//variable that stores today's date
var currentYear = todayDate.getFullYear();//variable that stores current year
var currentmonth = todayDate.getMonth();//variable that stores current month
var currentday = todayDate.getDay();//variable that store current day
var birthday = new Date(year, month, day);// generate date of birthday
var differentdate = (todayDate.getTime() - birthday.getTime()) /1000;//diffrence in time between the date of birth and the current one
//var y = 31622400;//the number of seconds in a leap year
const x = 31557600;//the number of seconds in a year
var passyear = Math.floor(differentdate / x);//variable that stores the years that have passed
var passday = Math.floor((differentdate % x) / 86400);//variable that stores the days that have passed
var numhours = Math.floor(((differentdate % x) % 86400) / 3600);//variable that stores the hours that have passed
var numminutes = Math.floor((((differentdate % x) % 86400) % 3600) / 60);//variable that stores the minutes that have passed
var numseconds = Math.floor((((differentdate % x) % 86400) % 3600) % 60);//variable that stores the seconds that have passed
//The condition checking the next birthday;
if (currentmonth > month) {
var futureDate = new Date((currentYear + 1), month, day);
} else if (currentmonth === month) {
if (currentday >= day) {
futureDate = new Date((currentYear + 1), month, day);
} else {
futureDate = new Date(currentYear, month, day);
}
} else {
futureDate = new Date(currentYear, month, day);
}
;
var nextbirth = (futureDate - todayDate) / 1000;
var nextday = Math.floor((nextbirth % x) / 86400);
var nexthours = Math.floor(((nextbirth % x) % 86400) / 3600);
var nextminutes = Math.floor((((nextbirth % x) % 86400) % 3600) / 60);
var nextseconds = Math.floor((((nextbirth % x) % 86400) % 3600) % 60);