I have been fighting with this for a bit now. I’m trying to convert epoch to a date object. The epoch is sent to me in UTC. Whenever you pass new Date()
an epoc
Addition to the above answer by @djechlin
d = '1394104654000';
new Date(parseInt(d));
converts EPOCH time to human readable date. Just don't forget that type of EPOCH time must be an Integer.
The Easiest Way
If you have the unix epoch in milliseconds, in my case - 1601209912824
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toString()
Sun Sep 27 2020 18:01:52 GMT+0530 (India Standard Time)
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toUTCString()
Epoch time is in seconds from Jan. 1, 1970. date.getTime()
returns milliseconds from Jan. 1, 1970, so.. if you have an epoch timestamp, convert it to a javascript timestamp by multiplying by 1000.
function epochToJsDate(ts){
// ts = epoch timestamp
// returns date obj
return new Date(ts*1000);
}
function jsDateToEpoch(d){
// d = javascript date obj
// returns epoch timestamp
return (d.getTime()-d.getMilliseconds())/1000;
}
Considering, you have epoch_time
available,
// for eg. epoch_time = 1487086694.213
var date = new Date(epoch_time * 1000); // multiply by 1000 for milliseconds
var date_string = date.toLocaleString('en-GB'); // 24 hour format