矩显示日期根据ISO-8601时区偏移

发布于 2025-02-09 17:10:47 字数 380 浏览 2 评论 0原文

我从API响应中获取ISO -8601日期字符串,如下所示:

var x1 = 2022-06-22T05:30:00+05:30

或者可能是

var x2 = 2022-06-22T08:30:00-05:00

浏览器时区,我应该显示日期,因为

X1 - 2022-06-22 05:30:30 IST
X2 - 2022-06-22 08:30:00 EST

我该如何从偏移中解析偏移时区,例如-05:00:005:00:005:00:005:30还是卢克森?

我尝试了MOMT.TZ(时间戳),但由于需要第二个参数,因此默认为UTC。

I am getting a ISO-8601 date string from an API response as follows :

var x1 = 2022-06-22T05:30:00+05:30

or it could be

var x2 = 2022-06-22T08:30:00-05:00

Irrespective of browser timezone I should display the dates as

X1 - 2022-06-22 05:30:30 IST
X2 - 2022-06-22 08:30:00 EST

How can i parse timezone from the offset like -05:00 or +05:30 using moment or luxon?

I tried moment.tz(timestamp) but it defaults to UTC since it needs the second argument.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2025-02-16 17:10:48

所以我做了更多的挖掘。

从逻辑上讲,我想要的是不可能的。

许多时区共享UTC的抵消。因此,可能会有歧义,如果我们尝试将偏移量转换为时区而没有任何其他信息,

因此我选择了一个解决方案,我的API响应在每个日期/时间字段中都会发送一个时区元数据。 (在后端,我有LAT/长信息可以在TZ中转换),

在前端,我只需使用时区信息将我的力矩对象格式化为所需的日期时间字符串即可。
例如

const timestring =  '2022-06-22T00:00:00+00:00'; 
const timezoneName = "America/Chicago" ;
moment.tz(timestring, timezoneName).format('YYYY-DD-MM hh:mm:ss z'); 
// Output: 2022-06-21 07:00:00 CDT

来源: https://stackoverflow.com/tags/timezone/timezone/info

So i did a bit more digging.

Logically what i want is not possible.

Many timezones shares UTC offset. Hence, there could be ambiguity, if we try to convert an offset to a TimeZone without any other additional info

Hence, I am opting for a solution, where my API response sends a timezone metadata for each date/time field. (I have lat/long info to convert in Tz in my backend)

In front End, i will simply use the timezone info to format my moment object into a desired date-time String.
For example

const timestring =  '2022-06-22T00:00:00+00:00'; 
const timezoneName = "America/Chicago" ;
moment.tz(timestring, timezoneName).format('YYYY-DD-MM hh:mm:ss z'); 
// Output: 2022-06-21 07:00:00 CDT

Source : https://stackoverflow.com/tags/timezone/info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文