Use Moment.js to convert Unix epoch time to human readable time

那年仲夏 提交于 2019-12-31 18:53:15

问题


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


回答1:


moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')



回答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"



回答3:


You can use moment.unix(epochTime).




回答4:


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>


来源:https://stackoverflow.com/questions/40752287/use-moment-js-to-convert-unix-epoch-time-to-human-readable-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!