根据用户所在的时区转换日期

编程入门 行业动态 更新时间:2024-10-22 11:31:54
本文介绍了根据用户所在的时区转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想根据他们的时区转换日期.

I have a date I want to convert based on their timezone.

比如我想设置在美国东部时间(美国/纽约)

For example, I want to set it in EST time (America/New_York)

2019-04-24 12:00:00

如果用户从 America/Los_Angeles 访问该网站,则会显示:

and if the user comes across the site from America/Los_Angeles, it will appear:

2019-04-24 09:00:00

我需要能够返回小时,所以在那个例子中:9.

I need to be able to return the hour, so in that example: 9.

我尝试使用 https://github/iansinnott/jstz 来确定他们的时区和https://moment.github.io/luxon 希望在没有运气的情况下处理转换.

I tried using https://github/iansinnott/jstz to determine their timezone and https://moment.github.io/luxon in hopes of handling the conversion w/o any luck.

我正在通过更改计算机上的时区进行测试,但运气不佳.

I was testing by changing the timezone on my computer w/o any luck.

推荐答案

根据时间转换日期可以这样完成.参考将日期转换为另一个时区示例代码段在下面.

Converting date based on the time can be done like this. reference convert date to another time zone example snippet is under.

var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
usaTime = new Date(usaTime);
console.log('USA time: '+usaTime.toLocaleString())


var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"});
usaTime = new Date(usaTime);
console.log('USA time: '+usaTime.toLocaleString())

这篇关于根据用户所在的时区转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 09:51:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1392005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   日期   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!