Green
Green

Reputation: 30795

Moment.js, how to just change a format of a date without changing timezone?

I want to change a format of a date and time string. But moment.js changes timezone to my system timezone (+3):

// This is a string:
"2013-09-20 23:59:59 +0100"

// I want to change it to this:
"20-09-2013 23:59:59 +0100"

// This is what I do and what I get. 1 hour is added by force:
moment("2013-09-20 23:59:59 +0100").format("DD-MM-YYYY HH:mm:ss ZZ")
"21-09-2013 01:59:59 +0300"

How to just change a format without changing timezone?

Upvotes: 6

Views: 7203

Answers (2)

Dixit
Dixit

Reputation: 13046

In moment.js v-2.8.3:

var dateTime = "2014-12-09 13:59:59 +0930";
var parseDateTimeZone = moment.parseZone(dateTime).format("YYYY-MM-DDTHH:mm:ssZ");

Doc-API

Upvotes: 5

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

See moment issue #887, directly regarding this. It may be easier in a future version, but the current workaround is as follows:

var input = "2013-09-20 23:59:59 +0100";
var m = moment(input).zone(input);
m.format("DD-MM-YYYY HH:mm:ss ZZ")

Upvotes: 5

Related Questions