从用户本地时间到UTC在网站上的日期/时间转换 [英] date / time conversion from user's local time to UTC on website

查看:134
本文介绍了从用户本地时间到UTC在网站上的日期/时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将一个不在办公室的系统添加到一个网站上,用户可以在其中标出不在办公室的日期和时间,这样他们可以提供另一个用户的信息,作为备份。

I'm currently adding an out of office like system to a website, where users will be able to mark their out of office dates and times such that they can provide another users's information to use as a backup while they are out.

我遇到的问题是将用户的本地时间转换为UTC。我已经看到通过向用户提供UTC来解决这个问题的其他帖子,并让客户端(js)将时间转换到当地时间。但是,我可以使用一个适合系统,我可以根据用户的时区偏好来转换日期服务器端。

The problem I have, is converting the users's local time to UTC. I've seen other posts that address this issue by supplying UTC to the user, and having the client (js) convert the time to and from local time. I do, however, have access to a propriety system I can use to convert the date server-side based on the user's time-zone preference.

我的问题是这样的:应该使用服务器端转换,这将允许提供用户的本地时间(例如,他们的美国时间,无论他们登录在哪里),或者我应该使用客户端转换?

My question is this: Should I use the server side conversion, which would allow a user's home local time to be supplied (say, their US time, regardless of where they are logged in), or should I use the client side conversion?

有没有人有这方面的经验?每个方法的一些不太明显的优点/缺点是什么?

Does anyone have any experience with this? What are some of the less obvious advantages / disadvantages of each method?

推荐答案

将所有内容存储在UTC中是一个很好的主意,建议您坚持下去。

Storing everything in UTC is a very good idea and I recommend that you stick to it.

有几种方法可以使用。我假设你会拥有用户的个人资料。您可以决定让用户选择他们喜欢的时区,并使用此信息显示所有内容。除了适当地处理所有用户输入(在服务器端进行转换,假设时区是首选的)。

There are few approaches you can use. I assume that you would have User's profiles. You may decide to give users an ability to pick the time zone they prefer and display everything using this information. As well as treat all user input appropriately (convert on server side, assuming the time zone is preferred one).

另一种处理它的方法是将用户的时间区域偏移通过 Date.getTimeZoneOffset()并以某种方式发送到您的服务器(即通过隐藏的表单字段或Ajax)。当你知道,在用户的时间显示一切将是一块蛋糕。在这种情况下,您可能希望在客户端上转换日期/时间,并以UTC(或使用带有时区偏移量的隐藏表单域)发送。

Another way to handle it, is to actually take User's time zone offset via Date.getTimeZoneOffset() and send it out to your server somehow (i.e. via hidden form field or Ajax). When you know that, showing everything in User's time would be piece of cake. In this case you might want to convert date/time on the client and send it out as UTC (or use hidden form field with time zone offset).

在这两种情况下您在服务器端处理数据库日期转换。这是我经验中最好的方法。我想指出,除了只是转换时区,你可能应该考虑以正确的格式显示日期/时间(即取决于AcceptLanguage头值)。

In both cases you handle database date conversion on server side. This is the best approach from my experience. I would like to point out, that apart from just converting time zones, you probably should think about displaying date/time in correct format (i.e. depending on AcceptLanguage header value).

这篇关于从用户本地时间到UTC在网站上的日期/时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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