简体   繁体   English

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

[英]moment-timezone interpret date in certain timezone

If I want to build a moment object, representing a timestamp in certain time zone, eg 2019/04/13 00:00:00 at time zone Europe/Berlin, how do I do it right? 如果我想建立一个表示特定时区的时间戳记的矩对象,例如欧洲/柏林时区的2019/04/13 00:00:00,我该怎么做?

What I'm trying to do: 我正在尝试做的是:

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

which results in m being set to Sat Apr 13 2019 02:00:00 GMT+0200 (Central European Summer Time) - 2 hours ahead of what I need. 这导致m设置为Sat Apr 13 2019 02:00:00 GMT+0200 (Central European Summer Time) -比我需要的时间早2小时。 What I need is 00:00:00 not 02:00:00. 我需要的是00:00:00而不是02:00:00。

What is the reasoning behind this behavior? 这种行为背后的原因是什么? How do I tell moment-timezone "take this date and time and interpret it as if it is in the time zone I say"? 我该如何告诉moment-timezone“采用此日期和时间,并将其解释为与我说的时区相同”?

Per your comment, following is a snippet that demonstrates how to apply the answer from the duplicate question. 根据您的评论,以下是演示如何应用重复问题答案的摘录。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM