I’m trying to parse epoch time in milliseconds to UTC timestamp. To achieve this I’m doing:
new Date([my variable in milliseconds]).toUTCString()
and this is returning the following:
When the correct date should actually be:
To recreate this you can use this code:
let dateInMills = 1655154491.8357913;
let parsedDate = new Date(1655154491.8357913).toUTCString();
console.log(parsedDate);
The number that I’m passing to the Date object is the exact same number that is being passed during the tests as it is static data.
More proof on the error:
running the code in the browser console
>Solution :
The Date constructor can take a number as it’s parameter. However, this number should be in milliseconds. In your case, you seams to have the time in second. You should multiply your input by 1000.
let dateInMills = 1655154491.8357913;
let parsedDate = new Date(dateInMills * 1000).toUTCString();
console.log(parsedDate);