我正在尝试将 utc 时间转换为本地时区,但没有成功。这是我的代码:
var utc = moment.tz('2017-03-28 15:00:00','UTC');
var s = utc.format();
var pl = moment.tz(utc.format(), 'Europe/Warsaw').format();
console.info(s);
console.info(pl);
输出是:
VM4496:4 2017-03-28T15:00:00Z
VM4496:5 2017-03-28T17:00:00+02:00
如您所见,Moment 比 UTC 增加了 2 小时,但 Europe/Warsaw
位于 UTC+1
时区
最佳答案
As you can see, moment adds 2 hours to UTC, but
Europe/Warsaw
is inUTC+1
time zone
不是 3 月 28 日。 3 月 28 日,由于夏令时,是 UTC+2,starts on March 26th in Warsaw并持续到 10 月 29 日。
OK, other question: how to make it to skip Daylight Savings time?
使用 Etc/GMT-1
时区。是的,确实是-1。这是可怕和错误的,但显然,这是 tz 数据库中的 GMT+1(!!!)(由 Moment-Timezone 使用,但不受 Moment-Timezone 控制),由于某些 POSIX 约定,该数据库使用该表示法。 Etc/GMT
时区的符号相反。啊。详情here和 here .
如果这让您感到不舒服,您可以添加自己的时区:
moment.tz.add([
'Own/UTC+1|+01|10|0|'
]);
示例:
moment.tz.add([
'Own/UTC+1|+01|-10|0|'
]);
var utc = moment.tz('2017-03-28 15:00:00','UTC');
var s = utc.format();
var plusOne = moment.tz(utc.format(), 'Etc/GMT-1').format();
var own = moment.tz(utc.format(), 'Own/UTC+1').format();
console.info(s);
console.info(plusOne);
console.info(own);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>
关于javascript - MomentJS - 将日期时间从 UTC 转换为不同的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42994421/