检测和存储Web应用程序客户端所在的时区的最佳方法是什么?

时间:2022-07-29 20:06:25

I have a multi-timezone web application that stores all of the datetime values in UTC in the database, when actions happen on the server, I can easily convert the time into UTC.

我有一个多时区Web应用程序,它将所有日期时间值以UTC格式存储在数据库中,当服务器上发生操作时,我可以轻松地将时间转换为UTC。

However, when a client enters a time or time span, what is the best way to detect and store it?

但是,当客户输入时间或时间跨度时,检测和存储它的最佳方法是什么?

I am currently doing the following:

我目前正在做以下事情:

  1. Get the value of Date.getTimezoneOffset() (javascript)
  2. 获取Date.getTimezoneOffset()(javascript)的值

  3. Post that to the server-side code via the ICallbackEventHandler on Page.
  4. 通过Page上的ICallbackEventHandler将其发布到服务器端代码。

  5. Store that value in the session
  6. 将该值存储在会话中

  7. On any subsequent request, calculate the output/input datetime value using the client's timezone.
  8. 在任何后续请求中,使用客户端的时区计算输出/输入日期时间值。

Regardless of the actual implementation, this seems like an in-elegant solution. Does anyone have a better method?

无论实际实现如何,这似乎都是一个优雅的解决方案。有没有人有更好的方法?

2 个解决方案

#1


4  

I was doing something very similar, but I now think I prefer to use javascript to convert all times to local on the client side. The server will give all times in UTC in the generated page, and the javascript will convert it once the page loads.

我正在做一些非常相似的事情,但我现在认为我更喜欢使用javascript将所有时间转换为客户端本地。服务器将在生成的页面中以UTC格式给出所有时间,并且一旦页面加载,javascript将转换它。

This eliminates confusion on the server side code, as I always know what time it is (UTC). On the client side I'm using jquery and the each() function to format all the time values at once. I write out each of the times as a unix time in a hidden field to make this easy to process with jquery.

这消除了服务器端代码的混淆,因为我总是知道它是什么时间(UTC)。在客户端,我使用jquery和each()函数一次格式化所有时间值。我将每个时间写为隐藏字段中的unix时间,以便使用jquery轻松处理。

The only problems I see with this method is that a) I don't have a real good date/time formatting routine yet in javascript, and b) if the user has javascript turned off, then it doesn't work.

我用这种方法看到的唯一问题是a)我在javascript中没有真正好的日期/时间格式化例程,而b)如果用户关闭了javascript,那么它就不起作用了。

#2


2  

You could use the "header" property of the HttpRequest Object to query the "If-Modified-Since" header sent by the client. This header should contain a date in a format that includes the timezone of the client, like this:

您可以使用HttpRequest对象的“header”属性来查询客户端发送的“If-Modified-Since”标头。此标头应包含一个格式的日期,该格式包含客户端的时区,如下所示:

If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT

a simple substring extraction will give you the timezone code. However, I'm afraid not all browsers are coherent in sending that header, so you should experiment a bit about it.

简单的子字符串提取将为您提供时区代码。但是,我担心并不是所有的浏览器在发送标题时都是一致的,所以你应该对它进行一些实验。

regards, Fabrizio

#1


4  

I was doing something very similar, but I now think I prefer to use javascript to convert all times to local on the client side. The server will give all times in UTC in the generated page, and the javascript will convert it once the page loads.

我正在做一些非常相似的事情,但我现在认为我更喜欢使用javascript将所有时间转换为客户端本地。服务器将在生成的页面中以UTC格式给出所有时间,并且一旦页面加载,javascript将转换它。

This eliminates confusion on the server side code, as I always know what time it is (UTC). On the client side I'm using jquery and the each() function to format all the time values at once. I write out each of the times as a unix time in a hidden field to make this easy to process with jquery.

这消除了服务器端代码的混淆,因为我总是知道它是什么时间(UTC)。在客户端,我使用jquery和each()函数一次格式化所有时间值。我将每个时间写为隐藏字段中的unix时间,以便使用jquery轻松处理。

The only problems I see with this method is that a) I don't have a real good date/time formatting routine yet in javascript, and b) if the user has javascript turned off, then it doesn't work.

我用这种方法看到的唯一问题是a)我在javascript中没有真正好的日期/时间格式化例程,而b)如果用户关闭了javascript,那么它就不起作用了。

#2


2  

You could use the "header" property of the HttpRequest Object to query the "If-Modified-Since" header sent by the client. This header should contain a date in a format that includes the timezone of the client, like this:

您可以使用HttpRequest对象的“header”属性来查询客户端发送的“If-Modified-Since”标头。此标头应包含一个格式的日期,该格式包含客户端的时区,如下所示:

If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT

a simple substring extraction will give you the timezone code. However, I'm afraid not all browsers are coherent in sending that header, so you should experiment a bit about it.

简单的子字符串提取将为您提供时区代码。但是,我担心并不是所有的浏览器在发送标题时都是一致的,所以你应该对它进行一些实验。

regards, Fabrizio