Getting client's TimeZone without using the normal JavaScript funtion 'getTimezoneOffset'?

℡╲_俬逩灬. 提交于 2021-01-29 18:46:25

问题


I am currently working on a registration form on a site, and I need to know exactly how to get the Timezone Offset for an active user-client to calculate his actual local time (no need for DST).

I've tried the JS method using the function getTimeZoneOffset() but it seems that it not very recommended because it is relying on the user Time Settings. (I changed the clock time on my machine and try it)

So any change in the user time settings will alter the timezone offset.

Are there any other 'robust' and 'trustworthy' methods that can I implement to get the timezone offset and then apply it inside my C# code.

Best.


回答1:


There's a lot to unwind here, so I'll go point by point of your question:

... I need to know exactly how to get the Timezone Offset for an active user ...

You can't get the time zone offset, you can only get a time zone offset. That is, the time zone offset you obtain is for a single point in time only. Time zones go through transitions, due to changes in standard time, and due to daylight saving time. When you call getTimeZoneOffset on a JavaScript Date object, that Date object is representing a particular point in time (by default, "now"). Thus, the offset returned is for that particular point in time. There is no guarantee that a previous point in time or a future point in time will use the same offset. Sending the offset to the server is only useful if you are pairing it with that specific timestamp (such as logging activities using a DateTimeOffset pattern).

Read also, the "Time Zone != Offset" section of the timezone tag wiki.

... to calculate his actual local time ...

You don't need to calculate that. new Date().toString() will give you the user's actual local time. If you need the output in a specific format, you can use libraries like Luxon, Date-fns, Moment.js, and others, or you can use a function like this one.

... (no need for DST).

People say this often because they are thinking it removes complexities, but the fact of the matter is - that's not up to you. Whether DST applies or not (and when) is up to the local government agency that regulates the specific time zone in question. If DST is applicable in the local time of your user at the time in question, and if you decide not to take that into account, then your results will be an hour off (usually).

Fortunately, all the hard work has already been done for you. A lot of people work diligently to ensure that time zone databases are kept accurate. It is easier to consume their efforts than to try to work around them.

Also, even if you think you don't care about daylight saving time, consider that many time zones have transitions created by changes in their standard time.

... getTimeZoneOffset() but it seems that it not very recommended because it is relying on the user Time Settings. ...

That it relies on user time settings is not a problem. The general reason it's not recommended is because of the "Time Zone != Offset" issue that I described already. If you want to know the user's time zone, you need a time zone identifier like "America/Los_Angeles", not an offset of 480.

... So any change in the user time settings will alter the timezone offset.

The user is in control of their own computer, and can set the time zone to anything they want. Usually people set the time zone to their local time, such that the clocks they see are correct. It's not unheard of for people to use other time zones, such as if they work overseas and want to align their work day to their parent company's local time (though personally, I would find that abhorrent). At any rate, it's not something you can reasonably expect to control.

... Are there any other 'robust' and 'trustworthy' methods that can I implement to get the timezone offset and then apply it inside my C# code.

If you want the user's local time zone, then use one of the methods listed here.

However, I'm not sure by what criteria you consider "robust and trustworthy". There's nothing particularly untrustworthy about the aforementioned methods...

One thing you might consider is if you actually don't want the user's local time zone, but rather the local time in a particular location. While these seem like the same thing, they're not always. You can derive the local time zone identifier of a location using one of the methods listed here.



来源:https://stackoverflow.com/questions/54025030/getting-clients-timezone-without-using-the-normal-javascript-funtion-gettimezo

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