Javascript display various time zones

旧街凉风 提交于 2019-12-06 09:48:22
Matt Johnson-Pint

First of all, the code you've shown just returns the current local time. It doesn't even attempt to change it for a specific time zone.

Secondly, you need to read the timezone tag wiki. In particular, read the section titled "Time Zone != Offset".

Now it just so happens that Arizona and Hawaii don't currently use daylight saving time, so you could adjust by offset if those were your only two concerns. But I'm sure you are looking for a more general solution.

To do it properly, you will need a library that implements the IANA time zone database. I list several of them here. For example, here is an example of displaying the current time in Los Angeles using moment.js with the moment-timezone plugin:

moment().tz("America/Los_Angeles").format("h:mm a")

If you're just looking for a quick and easy way to put a clock on your web site for a particular time zone, then I recommend using the free solution offered by timeanddate.com.

Write a function to move a Date by some offset in minutes/your choice

function offsetDate(offsetMinutes, d) {
    if (d) d = new Date(d);
    else d = new Date();
    if (offsetMinutes) d.setUTCMinutes(d.getUTCMinutes() + offsetMinutes);
    return d;
}
offsetDate(-10*60); // Thu Sep 05 2013 12:03:06 GMT+0100 (GMT Daylight Time)

Now use UTC functions to get the time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!