简体   繁体   English

JavaScript - 在特定时区使用单独的日期和时间值计算 UTC 值

[英]JavaScript - Compute UTC Value With Separate Date and Time Values in Specific Timezone

I am working with an API that returns the date and time of an event separately.我正在使用 API,它分别返回事件的日期和时间。 The date is always stored in YYYY-MM-DD format and the time is always stored in HH:MM format.日期始终以 YYYY-MM-DD 格式存储,时间始终以 HH:MM 格式存储。 The application is local to a geographic area on the US East Coast and the datetime stamp that the separate date and time values combine to convey will always be in EST.该应用程序是美国东海岸地理区域的本地应用程序,并且单独的日期和时间值结合起来传达的日期时间戳将始终使用 EST。

Using the date and the time values that the API gives me, I need to create a UNIX time value.使用 API 给我的日期和时间值,我需要创建一个 UNIX 时间值。 I can hard code the offset between EST and UTC using the following:我可以使用以下代码对 EST 和 UTC 之间的偏移量进行硬编码:

new Date(dateValue + 'T' + timeValue + '-04:00').getTime() / 1000

But the above doesn't take into account daylight savings and I'll need to update the hardcode -04:00 value with -05:00 during every daylight savings change.但是上面没有考虑夏令时,我需要在每次夏令时更改时用-05:00更新硬编码-04:00值。

I can compute the UTC value using:我可以使用以下方法计算 UTC 值:

new Date(dateValue + ' ' + timeValue + ' EST').getTime() / 1000

But this does not work for mobile Safari since its JS implementation of Date() does not recognize date strings in 2019-11-21 05:00 EST format.但这不适用于移动 Safari,因为其Date()的 JS 实现无法识别2019-11-21 05:00 EST格式的日期字符串。

How would I compute the UTC value here that is both daylight savings time aware and compatible on all modern browsers?我将如何计算这里的 UTC 值,它既能感知夏令时,又能在所有现代浏览器上兼容?

I would prefer a solution that does not require a 3rd party library.我更喜欢不需要 3rd 方库的解决方案。

Well, you really don't want to get into handling DST yourself, so I agree with adding 'EST' to the date string and then using the built-in Date parser.好吧,您真的不想自己处理 DST,所以我同意将“EST”添加到日期字符串,然后使用内置的日期解析器。 Too bad about Safari. Safari 太糟糕了。

You prefer to avoid a library, but how about a gist with a single js file that "Enhances Date.parse to support ES5 ISO-8601 strings in all environments" (with a loose definition of ISO date string)?您更愿意避免使用库,但是如何使用单个 js 文件来“增强 Date.parse 以在所有环境中支持 ES5 ISO-8601 字符串”(ISO 日期字符串的松散定义)的要点呢?

https://github.com/csnover/js-iso8601/tree/lax https://github.com/csnover/js-iso8601/tree/lax

You can consider using Intl.DateTimeFormat as mentioned in this answer.您可以考虑使用答案中提到的Intl.DateTimeFormat This handles timezones and daylight savings as well which I assume is what need:这也处理时区和夏令时,我认为这是需要的:

 const options = { year: '2-digit', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'Australia/Brisbane', timeZoneName: 'short' }, formater = new Intl.DateTimeFormat('en-US', options), startingDate = new Date('2012-04-10 10:10:30 -04:00'); const dateInNewTimezone = formater.format(startingDate); console.log(startingDate.toLocaleString()); console.log(dateInNewTimezone);

Please note the following is shown at my end:请注意以下显示在我的末尾:

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

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