Search code examples
momentjsunix-timestamp

Issue in converting Unix timestamp get from moment to normal date


I am using moment().valueOf() to get unix timestamp and to get the normal time from unix time i'm using moment.unix(unix_time).format('LLL') when the unix value is 1606985226404 getting the anser as May 2, 52893 6:36 AM which is incorrect. What is the issue with this?


Solution

  • According to the documentation of Moment.JS, moment() works with milliseconds.

    So:

    const unix_time = moment().valueOf(); // return epoc in milliseconds
    const now = moment.unix(unix_time / 1000).format('LLL').
    

    That's the trick.