MomentJS timestamp parsing shows wrong date

纵饮孤独 提交于 2020-01-06 09:02:34

问题


I am using moment JS in my Angular project and I tried to parse a timestamp I got from server.

the problem is that moment outputs : January 18, 1970 4:03 PM for the timestamp : 1526636521. which is not what I get form the online epoch converter. ( Friday, May 18, 2018 9:42:01 AM)

this is my moment call : moment.utc(data.TimeStamp).format('LLL') ;


回答1:


As the number you are using is number of seconds from 1970 Jan 1st,

moment.utc takes in number of milliseconds,

So either use,

moment.unix(1526636521).toString() // moment.unix takes in number of seconds moment.utc(1526636521000).toString() // Add three zeros to number.




回答2:


You have to use moment.unix instead of moment.utc

To create a moment from a Unix timestamp (seconds since the Unix Epoch), use moment.unix(Number).

var data = {
  TimeStamp: 1526636521
};
console.log( moment.unix(data.TimeStamp).format('LLL') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>


来源:https://stackoverflow.com/questions/50409233/momentjs-timestamp-parsing-shows-wrong-date

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