检测和存储 Web 应用程序客户端所在时区的最佳方法是什么? [英] What is the best way to detect and store the timezone the client of a web app is in?

查看:21
本文介绍了检测和存储 Web 应用程序客户端所在时区的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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. 获取 Date.getTimezoneOffset() 的值 (javascript)
  2. 通过页面上的 ICallbackEventHandler 将其发布到服务器端代码.
  3. 将该值存储在会话中
  4. 在任何后续请求中,使用客户端的时区计算输出/输入日期时间值.

不管实际实现如何,这似乎是一个优雅的解决方案.有人有更好的方法吗?

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

推荐答案

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

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.

这消除了服务器端代码的混乱,因为我总是知道现在是几点(UTC).在客户端,我使用 jqueryeach() 函数一次格式化所有时间值.我在隐藏字段中将每个时间写为 Unix 时间,以便使用 jquery 轻松处理.

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.

我发现这种方法的唯一问题是:

The only problems I see with this method is that:

a) 我在 javascript 中还没有很好的日期/时间格式化程序,并且

a) I don't have a really good date/time formatting routine yet in javascript, and

b) 如果用户关闭了 javascript,则它不起作用.

b) if the user has javascript turned off, then it doesn't work.

这篇关于检测和存储 Web 应用程序客户端所在时区的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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