矩显示日期根据ISO-8601时区偏移
我从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我做了更多的挖掘。
从逻辑上讲,我想要的是不可能的。
许多时区共享UTC的抵消。因此,可能会有歧义,如果我们尝试将偏移量转换为时区而没有任何其他信息,
因此我选择了一个解决方案,我的API响应在每个日期/时间字段中都会发送一个时区元数据。 (在后端,我有LAT/长信息可以在TZ中转换),
在前端,我只需使用时区信息将我的力矩对象格式化为所需的日期时间字符串即可。
例如
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