Kaxsp
Kaxsp

Reputation: 59

Moment JS is showing a wrong date

I am using Moment to get the proper date format.

The input timestamp is 1592082810093 . If you go to an online coverter ( https://www.freeformatter.com/epoch-timestamp-to-date-converter.html) you will see that format is correct and the date is 13/6/2020 23:13:30 .

So, having that input i am trying to visualize it in my react application.

When i Use this line of code:

moment.unix(`${scrape_lastdate.$date}`).format("DD-MM-YYYY hh:mm:ss")

It shows something but the date it shows is this one 25-07-52422 06:23:20 which is completely wrong.

If i use just below code without the .unix

moment(`${scrape_lastdate.$date}`).format("DD-MM-YYYY hh:mm:ss")

What i directly have is an "Invalid Date" as output.

Does anyone knows what could be the issue here? I am stuck on this.

Thank you very much!

Upvotes: 0

Views: 840

Answers (1)

segFault
segFault

Reputation: 4054

You should pass the timestamp as a number not a string.

Example:

moment(scrape_lastdate.$date).format("DD-MM-YYYY hh:mm:ss")

Check the function signature in the Moment.js docs

moment(Number)

Some additional information between the differences of those 2 functions from the docs.

moment(Number);

Similar to new Date(Number), you can create a moment by passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC).

moment.unix(Number)

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

Upvotes: 1

Related Questions