问题描述
我目前正在网站上注册表格上,我需要确切地知道如何为活动的用户-客户端获取时区偏移量,以计算其实际本地时间(无需DST).
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).
我已经尝试使用功能getTimeZoneOffset()来执行JS方法,但是由于它依赖于用户的时间设置,因此似乎不建议这样做.(我更改了机器上的时钟时间并尝试了)
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.
我是否可以实现其他鲁棒"和可信赖"方法来获取时区偏移量,然后将其应用于我的C#代码中.
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.
最好.
推荐答案
这里有很多要解决的问题,所以我将逐点讨论您的问题:
There's a lot to unwind here, so I'll go point by point of your question:
您无法获得 时区偏移,您只能获得 a 时区偏移.也就是说,您获得的时区偏移量仅针对单个时间点.由于标准时间的变化以及夏令时,时区会经过转换.当您在JavaScript Date
对象上调用 getTimeZoneOffset
时,该 Date
对象表示一个特定的时间点(默认为现在").因此,返回的偏移量是针对该特定时间点的.不能保证先前的时间点或将来的时间点将使用相同的偏移量.仅在将偏移量与特定时间戳配对时(例如,使用DateTimeOffset模式记录活动),将偏移量发送到服务器才有用.
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).
还请阅读时区标签Wiki 的时区!=偏移"部分.
Read also, the "Time Zone != Offset" section of the timezone tag wiki.
您无需进行计算. new Date().toString()
将为您提供用户的实际本地时间.如果您需要特定格式的输出,则可以使用 Luxon ,日期-fns ,时刻.js ,等等,或者您可以像这样使用函数 .
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.
人们之所以这么说是因为他们认为这样做可以消除复杂性,但是事实是- 这取决于您 .夏令时是否适用(何时适用)取决于当地政府机构,该机构负责规范所讨论的特定时区.如果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.
它依赖于用户时间设置不是问题.不建议这样做的一般原因是因为我已经描述了时区!=偏移"问题.如果您想知道用户的时区,则需要一个时区标识符,例如"America/Los_Angeles"
,而不是 480
的偏移量.
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
.
用户可以控制自己的计算机,并且可以将时区设置为所需的任何时间.通常,人们将时区设置为当地时间,以使他们看到的时钟是正确的.人们使用其他时区并不是闻所未闻的,例如,如果他们在国外工作,并希望将工作日与母公司的本地时间保持一致(尽管我个人会觉得很讨厌).无论如何,这不是您可以合理控制的.
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.
如果要使用用户的本地时区,请使用此处列出的方法之一.
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.
这篇关于是否在不使用常规JavaScript功能'getTimezoneOffset'的情况下获取客户端的TimeZone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!