MomentJS-将日期时间从UTC转换为不同的时区 [英] MomentJS - convert datetime from UTC to different timezone

查看:2150
本文介绍了MomentJS-将日期时间从UTC转换为不同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将utc时间转换为本地时区,但没有成功..这是我的代码:

I am trygin to convert utc time to local timezone with moment but without success.. Here is my code:

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

如您所见,片刻使UTC增加了2个小时,但Europe/Warsaw处于UTC+1时区

As you can see, moment adds 2 hours to UTC, but Europe/Warsaw is in UTC+1 time zone

推荐答案

如您所见,片刻使UTC增加了2个小时,但Europe/Warsaw处于UTC+1时区

不是在3月28日.由于夏令时,3月28日是UTC + 2,从3月26日开始.在华沙,并持续到10月29日.

Not on March 28th. On March 28th, it's UTC+2, because of Daylight Savings Time, which starts on March 26th in Warsaw and runs through October 29th.

好,另一个问题:如何使其跳过夏时制?

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时区的符号颠倒了.啊.详细信息此处

Use the the Etc/GMT-1 timezone. Yes, really -1. It's horrible and wrong, but apparently, that's GMT+1 (!!!) in the tz database (which is used by [but not controlled by] Moment-Timezone) which uses that notation because of some POSIX convention. The Etc/GMT timezones have their sign reversed. Ugh. Details here and here.

如果这让您感到不舒服,则可以添加自己的时区:

If that makes you uncomfortable, you could add your own timezone:

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>

这篇关于MomentJS-将日期时间从UTC转换为不同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆