最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

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?

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?

Share Improve this question asked Sep 6, 2013 at 11:51 GreenGreen 30.8k61 gold badges163 silver badges253 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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")

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

发布评论

评论列表(0)

  1. 暂无评论