user3676224
user3676224

Reputation: 873

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 = moment.tz("Mon Aug 22 23:32:59 +0000 2016", "ddd MMM DD HH:MM:SS ZZ YYYY", "UTC");
        var b = a.tz("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

UPDATE 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.

Upvotes: 0

Views: 4422

Answers (2)

Kane Blueriver
Kane Blueriver

Reputation: 4268

This works in my terminal:

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

What's wrong in your code is the minute, second and offset part. Check te document here: http://momentjs.com/docs/#/parsing/string-format/

Upvotes: 1

Sven
Sven

Reputation: 2889

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.

Upvotes: 2

Related Questions