Ka Tech
Ka Tech

Reputation: 9457

Javascript convert timezone Z to +0000 with moment

The backend API I am working with only understands timezone in +0000 format.

I currently I get a date in the current format:

"2017-12-20T16:39:31.000Z"

With moment.js how would I get it in the following string format?

2017-12-20T16:39:31+0000

So far I have done:

var date = new Date();
this.lastCheckedDate = moment(date).toISOString();

Upvotes: 2

Views: 4488

Answers (1)

31piy
31piy

Reputation: 23859

You can use the .format() method to format the date in your own way:

moment("2017-12-20T16:39:31.000Z").format('YYYY-MM-DDTHH:mm:ssZZ');

If the date is in another timezone, I recommend using .utc() first to convert the timezone to UTC, and then format the date:

moment("2017-12-20T16:39:31.000+0530").utc().format('YYYY-MM-DDTHH:mm:ssZZ');

Upvotes: 5

Related Questions