根据用户所在的时区转换日期 [英] convert date based on timezone user is in

查看:27
本文介绍了根据用户所在的时区转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.com/iansinnott/jstz 来确定他们的时区和https://moment.github.io/luxon 希望在没有运气的情况下处理转换.

I tried using https://github.com/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆