Hello, I have created this program guided by one seen on google, but when it comes to the result it gives me 0, 0, 0, 0. On the other hand, I cannot find a way to put the years, months, days and hours elapsed. I mean adding the years to what I already have, but giving time well, not what it gives me now. What am I doing wrong and what should I add? Well, with what I have I would only get days, hours and minutes. As I say, I also wanted to put the years. Thanks.
var msegMinuto = 1000 * 60;
var msegHora = msegMinuto * 60;
var msegDia = msegHora * 24;
var nacimiento = new Date(1965, 7, 20)
var hoy = new Date()
nacimiento.setMonth()
nacimiento.setDate()
nacimiento.setHours()
var tiempo = hoy - new Date()
//calcular dias
var dias = Math.floor(tiempo / msegDia)
tiempo = tiempo - (dias * msegDia)
//calcula horas
var horas = Math.floor(tiempo / msegHora)
tiempo = tiempo - (horas * msegHora)
var minutos = Math.floor(tiempo /msegMinuto)
tiempo = tiempo - (minutos * msegMinuto)
console.log("Han pasado " + dias + "dias, " + horas +" horas, y"+ minutos + "minutos desde que naciste.')
Greetings, I leave you the answer to your question.
first the month, day and year you were assigning it to the date method of a variable called birth but that is not where you occupy it
second you were concatenating the values wrong, I leave you in the following code how to do it correctly
third and last the result of the var time you were subtracting it backwards first it should be new Date() - today and you had it backwards which caused it to come out in the tests as negative
The problem seems to be that you are not calculating the time difference between the current moment and the birth, which I assume is what you want to do. Instead of getting a new Date() object when doing the subtraction, you should subtract the two objects you already had. By the way, I don't know what you intended to do using the setMonth, setDate and setHours methods but what you got is to delete the date data (or more specifically set it to NaN) by not passing any value to functions that require one, surely this program survive without those lines although I would like you to answer me what its purpose was.