oosniss oosniss - 1 year ago 110
Javascript Question

momentjs unix timestamp converts pm to am

I am having a problem with Moment.js.

Here are the codes.

var date = "2016-07-26 06:15 pm";
var unixDate = moment(date).unix();
var renderDate = moment.unix(unixDate).format("YYYY-MM-DD hh:mm a");

I want the value of "renderDate" to be same as the value of "date" variable. However, what gets printed out in the console is "2016-07-26 06:15 am".

I am completely lost as to why everything remains the same except "am/pm" at the end.

Answer Source

unix method accepts only number

You need to parse date before usage

moment('2016-07-28 06:15 PM', 'YYYY-MM-DD hh:mm a').format('YYYY/MM/DD hh:mm a')