时刻时区解释特定时区的日期



如果我想构建一个时刻对象,表示特定时区的时间戳,例如欧洲/柏林时区的 2019/04/13 00:00:00,我该怎么做?

我想做什么:

moment.tz.setDefault('Europe/Berlin');
const m = moment('2019/04/13 00:00:00');

这导致m设置为比我需要的提前 Sat Apr 13 2019 02:00:00 GMT+0200 (Central European Summer Time) - 2 小时。我需要的是 00:00:00 而不是 02:00:00。

这种行为背后的原因是什么?我如何告诉时刻时区"获取此日期和时间并将其解释为好像在我说的时区中"?

根据您的评论,下面是一个片段,演示了如何应用重复问题的答案。

const berlin = moment.tz('2019/04/13 00:00:00', 'YYYY/MM/DD hh:mm:ss', 'Europe/Berlin');
console.log('UTC', berlin.utc().format());
// UTC 2019-04-12T22:00:00Z
console.log('Europe/Berlin', berlin.tz('Europe/Berlin').format());
// Europe/Berlin 2019-04-13T00:00:00+02:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>

最新更新