moment.js format for unix timestamp?

11,876

Solution 1

have you tried moment(date).format('MM/DD/YYYY hh:mm a') as is?

Solution 2

I believe there is a very simple fix for this when using moment. Simply use the .unix function with moment. Below is an example -

var date = moment.unix(date).format("MM/DD/YYYY hh:mm a")
Share:
11,876
NullReference
Author by

NullReference

Yep, I'm that interesting.

Updated on June 04, 2022

Comments

  • NullReference
    NullReference almost 2 years

    I'm trying to convert a unix timestamp to local time using moment.tz . I've noticed I have to use a format when passing in a date, like this

    moment.tz(date, 'MM/DD/YYYY hh:mm a', zone).format('MM/DD/YYYY hh:mm a');
    

    My problem is when I want to convert from a unix timestamp to calendar like date the conversion fails. What should I pass for the format string?

    I've tried these two formats with no luck

    var date = moment.tz(publishInterval.DateUnix, 'X', zone).format('MM/DD/YYYY hh:mm a');
    
    
    var date = moment.tz(publishInterval.DateUnix, zone).format('MM/DD/YYYY hh:mm a');
    
  • Matt Johnson-Pint
    Matt Johnson-Pint about 8 years
    This will work if the date variable is a number, and is in terms of milliseconds. (Not sure if that's what the OP has or not yet.)
  • Pelicer
    Pelicer over 2 years
    Maybe adding some event listener or HTML code that runs your JS on run would be in order to show your code running.