js find out how many years have passed since the specified date. There are extra 60 years
const date1 = new Date(2000, 12, 4);
console.log(new Date(new Date().getTime() - date1.getTime()).getYear());
I'm trying to get how many years have passed from a given date to the current moment. But for some reason, the result is 60 years longer than it should be.
0
1 answers
This question is equivalent to calculating the age. The answer is, for example, here: https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd
Here is an example (https://stackoverflow.com/a/7091965/5122436):
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
0
Author: Mikhail Ionkin, 2020-08-28 16:41:17