user3676224 user3676224 - 1 year ago 64
Javascript Question

Moment time zone format giving invalid date

I have the following time and date in this formate

Mon Aug 22 23:32:59 +0000 2016

and I want to convert it to los angeles time, however A) it gives out invalid date. and B) if i remove the last parameter "UTC" it gives out the right time but wrong minutes.

I am passing it to moment time zone as follows

var a ="Mon Aug 22 23:32:59 +0000 2016", "ddd MMM DD HH:MM:SS ZZ YYYY", "UTC");
var b ="America/Los_Angeles");
console.log(b.format("YYYY-MM-DD HH:MM A"));

But its giving me
Invalid date
and I am unable to figure out the issue.
any clarification would be helpful

fixed the issue of invalid date

var a = moment("Mon Aug 22 23:32:59 +0000 2016","ddd MMM DD HH:mm:ss ZZ YYYY");

Also fixed the moments issue, which was due to the fact that i was using capital HH and MM rather than hh:mm which i should've been using.

Answer Source

According to the documentation, your format string is wrong.

console.log(b.format("YYYY-MM-DD HH:mm A"));

It shows 08 for minutes, as MM (uppercase M) is the token for Months (August = 08), while mm is the token for minutes. This is also the reason with the Invalid date, it tried to parse the minutes as months, but couldn't. In the Update your capitalization is correct.