简体   繁体   English

日期选择器获取给定时区的日期而不是本地时间

[英]Date Picker get Date in given timezone instead of local time

In my react application, when i creating a new date object for date-picker using moment-timezone package(with different timezone), the time is automatically converting to the local system time + the chosen timezone time.在我的反应应用程序中,当我使用 moment-timezone 包(具有不同的时区)为日期选择器创建新的日期 object 时,时间会自动转换为本地系统时间 + 选择的时区时间。 Is there any way to create a new date object with set a timezone on client side?有没有办法在客户端设置一个时区来创建一个新的日期 object ? i am using moment-timezone for globalization and Meteor Js for back-end我使用时刻时区进行全球化和 Meteor Js 用于后端

If I understand correctly, your issue is that you want to provide a Date Picker UI that automatically generates a Date object from the selected date and user time zone setting (retrieved from your database), but what you get is a date in user's local time (hence may already have some timezone offset) + the user time zone offset setting.如果我理解正确,您的问题是您想提供一个日期选择器 UI,该 UI 会根据所选日期和用户时区设置(从您的数据库中检索)自动生成日期 object,但您得到的是用户本地时间的日期(因此可能已经有一些时区偏移)+用户时区偏移设置。

You have mainly 3 possible solutions:您主要有 3 种可能的解决方案:

  1. Have the Date Picker UI always provide a date in UTC (typically at midnight UTC), then add your user's timezone offset setting.让日期选择器 UI 始终提供 UTC 日期(通常在 UTC 午夜),然后添加用户的时区偏移设置。
  2. Have the Date Picker UI take your user's timezone setting as a configuration, so that when it automatically generates a Date, it uses that timezone offset configuration.让日期选择器 UI 将用户的时区设置作为配置,以便在自动生成日期时使用该时区偏移配置。
  3. Have the Date Picker UI provide a Date in the browser local time, then shift it to UTC, then add your user's timezone offset setting.让日期选择器 UI 在浏览器本地时间中提供日期,然后将其转换为 UTC,然后添加用户的时区偏移设置。

Option 1 DatePicker in UTC: I have not seen any library that provides such option, but it does not sound too weird, so you may find one that does. UTC 中的选项 1 DatePicker:我还没有看到任何提供此类选项的库,但听起来并不奇怪,因此您可能会找到一个提供此类选项的库。 Or you may fiddle with the component code... which is probably overkill compared to option 3.或者您可能会摆弄组件代码......与选项 3 相比,这可能是矫枉过正。

Option 2 DatePicker with timezone configuration: from your comment, this sounds like what you expect.带有时区配置的选项 2 DatePicker:从您的评论来看,这听起来像您所期望的。 Unfortunately, I have not seen any library that offers such feature either.不幸的是,我也没有看到任何提供此类功能的库。 As this sounds even more specific than option 1, it is unlikely you can find such a library.由于这听起来比选项 1 更具体,因此您不太可能找到这样的库。

Option 3 local time shift to UTC then add user timezone: this is the standard practice to achieve the objective of generating a Date in a given timezone, starting from a Date in local time, due to all DatePicker UI working just with local time to simplify things.选项 3 本地时间转移到 UTC 然后添加用户时区:这是实现在给定时区生成日期的目标的标准做法,从本地时间的日期开始,因为所有 DatePicker UI 只使用本地时间来简化事物。 As to shift local time to UTC, you should have plenty questions and answers on SO already.至于将本地时间转换为 UTC,您应该已经有很多关于 SO 的问题和答案。

In case you have to provide an initial value to your Date Picker UI (typically a previous value from database), then make sure to perform the reverse operation, ie subtract your user's timezone offset setting to get a date in UTC, then shift to browser locale time.如果您必须为日期选择器 UI 提供初始值(通常是数据库中的先前值),请确保执行反向操作,即减去用户的时区偏移设置以获取 UTC 日期,然后切换到浏览器语言环境时间。 I have seen applications that neglect this part and have incorrect date when the local time has a negative offset to UTC.当本地时间与 UTC 有负偏移时,我已经看到忽略这部分并且日期不正确的应用程序。

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

相关问题 Javascript将给定时区中的日期转换为本地日期 - Javascript translate date in a given timezone to local date 忽略本地时区的角度的日期选择器 - Date Picker for Angular that Ignores Local Timezone 如何使用给定时区偏移量的时刻获取日期和时间 - How to get date and time using moment for a given timezone offset 实例化一个日期 object,给定时间作为字符串和时区? - Instantiate a Date object, given time as a string and a timezone? 日期和时间的问题是取决于本地时区的对话 - Issue with date and time is conversation depending on local timezone 如何在JS中为日期选择器而不是浏览器设置时区 - How to set Timezone for Date Picker instead of browser in js 时间和日期选择器 type='datetime-local' - Time and Date picker type='datetime-local' 如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC - How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js 如何使用选定的下拉菜单使用受尊重的时区进行日期时间选择器? - How to do date time picker with respected timezone with selected dropdown? 如何在js中使用本地时区将日期字符串更改为日期时间? - How to change the date string to date time with local timezone in js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM