简体   繁体   English

javascript显示中的时区偏移量

[英]timezone offset in javascript display

I'm running into what seems to be a common problem of using a javascript datepicker to display and allow users to select dates from a calendar of available schedule dates for room reservations. 我正在遇到使用javascript日期选择器显示的常见问题,并允许用户从房间预订的可用日程安排日期中选择日期。

The browser's timezone conversion means that these dates are always off by some margin, so that often when displaying a reserved date to the user, the date will fall on "tomorrow" to the viewer, when the server (the timezone local to the asset or room as stored in the DB) shows them as "today". 浏览器的时区转换意味着这些日期总是偏离一些余量,因此通常在向用户显示保留日期时,日期将“明天”落在查看器上,当服务器(资产的本地时区或存储在DB中的房间将它们显示为“今天”。

I would like the user's browser to ignore the javascript date conversion and just use the actual datetime that is passed in by the database. 我希望用户的浏览器忽略javascript日期转换,只使用数据库传入的实际日期时间。

However, this even happens on a really simple example like this: 然而,这甚至发生在一个非常简单的例子上:

var date = '2013-02-05';

var newdate = new Date(date);

console.log(newdate); // Mon Feb 04 2013 16:00:00 GMT-0800 (PST) 

It appears that the date variable is assumed by the browser to be GMT, and when I create a javascript date object from it, it converts that GMT time to my local time. 似乎浏览器假定date变量为GMT,当我从中创建javascript日期对象时,它将GMT时间转换为我当地时间。

Is the best practice in this case to use GMT dates in the database, and set the site's local time offset as a variable in the javascript, which can then be used to offset the dates displayed to the end user, and again offset the dates received from the end user for insertion into the database? 在这种情况下,最佳做法是在数据库中使用GMT日期,并将站点的本地时间偏移设置为javascript中的变量,然后可以使用该变量来偏移显示给最终用户的日期,并再次抵消收到的日期从最终用户插入数据库?

This is confusing since there's so many potential pitfalls-- the PHP locale, mysql locale, or browser's locale could all factor into it and mess up the final date. 这是令人困惑的,因为存在很多潜在的陷阱 - PHP语言环境,mysql语言环境或浏览器的语言环境都可能会影响它并弄乱最终日期。 Any advice on ensuring a consistent date value appreciated! 任何关于确保一致日期值的建议都值得赞赏!

Good question, handling timezones is a mess. 好问题,处理时区是一团糟。 Fortunately Javascript has UTC variants of most of its methods. 幸运的是,Javascript的大多数方法都有UTC变体。 See http://en.wikipedia.org/wiki/Coordinated_Universal_Time http://en.wikipedia.org/wiki/Coordinated_Universal_Time

I think the best is to use UTC dates everywhere (except UI maybe ). 我认为最好是在任何地方使用UTC日期(除了UI 可能 )。 Make sure that server uses and stores UTC dates, and use the javascript UTC methods everywhere. 确保服务器使用和存储UTC日期,并在任何地方使用javascript UTC方法。 That's the first and most important step, so you know the dates are consistent. 这是第一步也是最重要的一步,所以你知道日期是一致的。

How you present dates in UI is lot less straightforward, and kind of depends on the target audience, application nature etc. I think that's more a subject of discussion, and kind of unanswerable in Stack Overflow (there are other forums for subjective pondering). 你如何在UI中呈现日期要简单得多,而且取决于目标受众,应用程序性质等等。我认为这更像是一个讨论主题,而且Stack Overflow中还有一些无法回答的问题(还有其他主观思考论坛)。

I'd say don't trust browser or any kind of geolocation to autoconvert timezones; 我会说不信任浏览器或任何类型的地理定位来自动转换时区; it should be user configurable, or maybe group/project/install specific setting. 它应该是用户可配置的,或者可能是组/项目/安装特定的设置。 Some software are mainly used inside one timezone, and trying to automatically convert timezones might be confusing and annoying for users, if they are used to communicate in some "standard" time. 某些软件主要在一个时区内使用,如果用户在某些“标准”时间内进行通信,尝试自动转换时区可能会让用户感到困惑和烦恼。 Should user see times in different timezone if he or she is travelling? 如果用户正在旅行,用户是否应该看到不同时区的时间? Sometimes it makes sense, sometimes not, but at least make sure the user knows what logic you are following, both when reading and inputting times. 有时它是有意义的,有时不是,但至少确保用户知道你在阅读和输入时间时遵循的逻辑。

I develop project management software where it's important these things are handled unambiguously bot by the application, and by users (!). 我开发项目管理软件,重要的是这些东西由应用程序和用户(!)明确地处理。 My approach is to always force UTC both for display and input. 我的方法是始终强制UTC显示和输入。 It's also clearly visible in each date that it is UTC. 它在UTC的每个日期也清晰可见。 Handling a variety of timezones would get hairy quick, and I decided it's better to just not do that. 处理各种各样的时区会很快变得毛茸茸,我决定不这样做会更好。 I have some helpers in parts of the UI that show the same info in users local timezone etc in fine print. 我在UI的一些部分中有一些帮助,它们在用户本地时区等中以精细打印显示相同的信息。 There is a project-wide setting to "hide all timezone-related stuff, and force timezone x", that can be used in smaller projects that are known to never cross timezone boundaries, there is an agreement to use certain zone, or it's just a given and its better to not bother users with that kind of complexity. 有一个项目范围的设置“隐藏所有与时区相关的东西,并强制时区x”,可用于已知永远不会跨越时区边界的小型项目,有使用某个区域的协议,或者它只是一个给定的,更好的是不打扰那种复杂性的用户。

EDIT: I should add that, as an example how hairy this can get, sometimes a time is only a time. 编辑:我应该补充一点,作为一个例子,这可以得到多毛,有时一段时间只是一个时间。 In some contexts, an event at 15:00 could mean it happens at 15:00 at different places, at their respective local timezones. 在某些情况下,15:00的事件可能意味着它发生在15:00的不同地方,在各自的当地时区。 Uhh.... 呃....

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

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