Use Moment.js to convert Unix epoch time to human readable time
62,397
Solution 1
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
Solution 2
From the Docs: Unix Timestamp
var day = moment.unix(1318781876); //seconds
var day = moment(1318781876406); //milliseconds
// and then:
console.log(day.format('dddd MMMM Do YYYY, h:mm:ss a'));
// "Sunday October 16th 2011, 9:17:56 am"
Solution 3
You can use .format('LLLL')
for your requirement.
let result = moment(epoch).format('LLLL');
let epoch = 1562127342123;
let result = moment(epoch).format('LLLL');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
Solution 4
You can use moment.unix(epochTime)
.
Author by
Admin
Updated on May 22, 2020Comments
-
Admin almost 4 years
I'm trying to use Moment.js to convert a Unix epoch time to a date and time. I'd also like to know how to have it formatted like below.
Tuesday, November 22, 2016 6:00 PM
-
webnoob over 6 yearsNOTE: The above is backwards,
moment.unix(timestamp)
is for seconds andmoment(timstamp)
is for milliseconds. Not the other way round as the answer describes (at least in the latest version of moment). -
Keno over 6 years@webnoob Thanks ! , changed it.
-
Arthur Tarasov about 6 yearsyou can also break it with with HTML, which I think is pretty cool:
...format('dddd, <br> MMMM Do, YYYY <br> h:mm:ss A')