简体   繁体   English

日期和时区(javascript,java,struts)

[英]Dates and time zones (javascript, java, struts)

I am sure this is a very very common problem. 我确信这是一个非常普遍的问题。

I have a struts action that uses a common jquery datepicker calendar to select a date for my code to use. 我有一个struts动作,该动作使用通用的jquery datepicker日历为我的代码选择要使用的日期。 Problem is that our production servers live in Texas on CST, but I am in DC on EST. 问题是我们的生产服务器位于CST的德克萨斯州,但我位于EST的DC。 My web browser sends 12/6/11 12:00 AM EST (midnight) and it is interpreted as 12/5/11 11:00 PM CST by the server. 我的网络浏览器发送了美国东部标准时间12/6/11 12:00(午夜),服务器解释为CST 12/5/11 11:00 PM。

Any recommended ways to fix this? 有建议的解决方法吗?

Should I send the local browser's timezone as a parameter to my web services code? 我是否应该将本地浏览器的时区作为参数发送到我的Web服务代码?

If possible, always deal with UTC times internally in your code (and especially in communications between components). 如果可能,请始终在代码内部(尤其是组件之间的通信中)处理UTC时间。 Convert to local time at the last possible moment before display, where you know what the user's time zone is. 在显示之前的最后一个可能时刻转换为本地时间,在此您可以知道用户的时区。

In your case, convert the timestamp to UTC in the browser before sending to the server. 您的情况是,在发送到服务器之前,先在浏览器中将时间戳转换为UTC。

If you're doing something like setting appointment times in the future, beware of Daylight Saving Time, where everything gets shifted by an hour twice a year (but UTC doesn't). 如果您将来要进行诸如设置约会时间之类的操作,请当心“夏令时”,该时间每年两次将所有内容每小时移位一个小时(但UTC不会)。 For example, the user will expect that setting an appointment for "next Tuesday at 2:00 pm" will work even if next Tuesday is a different UTC offset from now. 例如,即使下个星期二与现在的UTC偏移时间不同,用户也希望为“下个星期二2:00 pm”设置约会将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM