时刻.js,如何在不更改时区的情况下更改日期格式

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

本文关键字:时区 情况下 格式 日期 js 时刻      更新时间:2023-09-26

我想更改日期和时间字符串的格式。但是时刻.js将时区更改为我的系统时区(+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"

如何在不更改时区的情况下更改格式?

请参阅时刻问题 #887,直接与此有关。 在将来的版本中可能会更容易,但当前的解决方法如下:

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

很快.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");

文档接口