I am trying to make a function that is, effectively, the opposite of getTime(); it takes a number of milliseconds as an input and converts the number into years, days, hours, seconds, and milliseconds, and then returns a Date object with those values. The conversion works perfectly, but when I try to create the Date object, things go wrong.
The code for the conversion itself is this:
function milliConvert(milliseconds){
const second = 1000;
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
let years2 = Math.floor(milliseconds / year);
let days = Math.floor(milliseconds / day)-(years2*365);
let hours = Math.floor((milliseconds / hour)-(((years2*365))*24+(days*24)));
let minutes = Math.floor((milliseconds / minute)-(((years2*365))*24*60+60*(days*24)+((hours)*60)));
let seconds= Math.floor((milliseconds / second)-((years2*365*24*60*60)+(days*24*60*60)+(hours*60*60)+(minutes*60)));
let outMilli= Math.floor((milliseconds)-((years2*365*24*60*60)+(days*24*60*60)+(hours*60*60)+(minutes*60)+(seconds*1000)));
console.log(years2+":"+days+":"+hours+":"+minutes+":"+seconds)
//the rest of this function is in the next code snippet
Like I said earlier, it works as expected. But I had to do a lot of troubleshooting with the code for converting it into a Date object, and it still doesn’t work.
My first iteration would always give me a large negative number, but I realized that that was because the years2 value was below 1970, so I made sure to increment it by 1970. After that, it still didn’t work, because the day and hour values had been, for some unknown reason, increased by 30 and 5 respectively. My current code for creating the date object is this:
return(new Date(years2+1970,1,days-30,hours-5,minutes,seconds,outMilli));
}
It works fine with smaller dates, but if I set milliseconds to Date.now(), the value of the date is “2078-08-14T17:48:43.764Z” (according to the VSCode console, when the command was run the time and date were 2024-04-27 15:29:03). My goal is for milliConvert(x).getTime() to equal X. I am aware that other people have answered similar questions before, but I would still like to know why this specifically doesn’t work, as I think it will help further my understanding of the Date object.
Thanks in advance!
Invincible_Atlas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1