问题
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