Kaki Master Of Time
Kaki Master Of Time

Reputation: 1614

MomentJS timestamp parsing shows wrong date

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') ;

Upvotes: 0

Views: 1176

Answers (2)

Basavaraj Bhusani
Basavaraj Bhusani

Reputation: 5673

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.

Upvotes: 2

VincenzoC
VincenzoC

Reputation: 31482

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>

Upvotes: 2

Related Questions