简体   繁体   English

使用 moment.js 和 moment 时区将日期、时间和偏移量转换为 ISO8601 DateTime

[英]Converting a date, time and offset into an ISO8601 DateTime using moment.js and moment timezone

I am processing some itinerary data where the times and dates are all provided in the local timezone.我正在处理一些行程数据,其中时间和日期都以当地时区提供。 I am adding this data to a database where I'd like to store all of the dates in UTC, with the proper timezone offset.我正在将此数据添加到数据库中,我想在其中以 UTC 格式存储所有日期,并带有适当的时区偏移量。 I'm trying to process these dates with moment.js.我正在尝试使用 moment.js 处理这些日期。

The input string for date/time is formatted like this 2020-07-12 13:00 and the timezone is in this format Europe/Amsterdam .日期/时间的输入字符串格式如下2020-07-12 13:00 ,时区格式为Europe/Amsterdam

I want to end up with a string like:我想得到一个字符串,如:

2020-07-12T11:00:00+02:00

The trouble I'm having, is that moment converts my input time to either local time or utc if I use the .utc() method.我遇到的麻烦是,如果我使用.utc()方法,那一刻会将我的输入时间转换为本地时间或 utc。

This code is getting me the correct result, but I don't understand why and I'm not sure if I can rely on its accuracy:这段代码让我得到了正确的结果,但我不明白为什么,我不确定我是否可以依赖它的准确性:

var offset = moment.tz(`Europe/Amsterdam`).utcOffset();
var m = moment(`2020-07-12 13:00`, 'YYYY-MM-DD HH:mm').utc().subtract(240 + offset + offset, 'minutes').utcOffset(offset); // (240 is my own UTC offset)

How can I simply input a date, time and timezone and end up with a correct ISO8601 DateTime?如何简单地输入日期、时间和时区并最终得到正确的 ISO8601 日期时间?

If you are already using Moment and Moment-TimeZone in your app, then you can simply do the following:如果您已经在您的应用程序中使用 Moment 和 Moment-TimeZone,那么您可以简单地执行以下操作:

const m = moment.tz('2020-07-12 13:00', 'YYYY-MM-DD HH:mm', 'Europe/Amsterdam');
m.format() //=> "2020-07-12T13:00:00+02:00"

However, the Moment team recommends using Luxon for new development.但是,Moment 团队建议使用Luxon进行新开发。 The equivalent is:等效的是:

const dt = luxon.DateTime.fromFormat('2020-07-12 13:00', 'yyyy-MM-dd HH:mm', { zone: 'Europe/Amsterdam'});
dt.toISO()  //=> "2020-07-12T13:00:00.000+02:00"

The only difference being that milliseconds are included.唯一的区别是包括毫秒。 You can use a different formatting function if you prefer a different output.如果您喜欢不同的 output,您可以使用不同的格式 function。

The main benefit of Luxon is that it uses the built-in time zone functionality provided by the ECMAScript Internationalization API, whereas Moment-Timezone bundles its own time zone data - which can be quite large. Luxon 的主要好处是它使用了 ECMAScript 国际化 API 提供的内置时区功能,而 Moment-Timezone 捆绑了自己的时区数据——这可能非常大。

Also, note that in your question by asking for 2020-07-12T11:00:00+02:00 you seem to be misunderstanding the ISO 8601 format.另外,请注意,在您询问2020-07-12T11:00:00+02:00的问题中,您似乎误解了 ISO 8601 格式。 In that format, the time presented is the local time.在这种格式中,显示的时间当地时间。 Thus, it should be 13:00 , not 11:00 .因此,它应该是13:00 ,而不是11:00 The +02:00 means, "this was the offset from UTC for this local time ". +02:00表示“这是本地时间与 UTC 的偏移量”。 (It doesn't mean that you apply the offset to get the local time.) (这并不意味着您应用偏移量来获取本地时间。)

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

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