强制react-datepicker使用UTC

发布于 2025-01-17 08:20:55 字数 219 浏览 3 评论 0原文

如果选择,则开始时为 null。

当我选择日期 20/04/2022 时,

Datepicker 显示 20/04/2022

Console.log 显示 Wed Apr 20 2022 00:00:00 GMT+0200(中欧夏令时间),

但表单上的有效负载显示 2022-04- 19T22:00:00.000Z

我可以将格式锁定为 UTC 吗?

If selected starts out as null.

When I select a date 20/04/2022

Datepicker shows 20/04/2022

Console.log shows Wed Apr 20 2022 00:00:00 GMT+0200 (Central European Summer Time)

But my payload on the form shows 2022-04-19T22:00:00.000Z

Can I lock the format to UTC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦慕雪 2025-01-24 08:20:55

我想完全忽略用户的本地时区,甚至不使用它来显示或发送到 API。为了做到这一点,我使用他们的时区来解析来自我的目标 api 时区 (utc) 的日期。这对我有用:

它在 momentjs b/c 中,我们的代码很旧,可以在较新的库中复制

  const { timeZone: localTZ } = Intl.DateTimeFormat().resolvedOptions()
  // The TZ format I want everything to be displayed and submitted as
  const apiTZ = 'utc'
  const [apiTime, setApiTime] = useState(moment.utc())

  <DatePicker
    onChange={date => {
      const apiTimeFromPickerLocalDate = moment(date)
        .tz(localTZ)
        // change TZ without changing date/time
        .tz(apiTZ, true)
        .format()        
      setApiTime(apiTimeFromPickerLocalDate)
    }}
    selected={
      moment(apiTime)
        .tz(apiTZ)
        // Display picker time in local TZ without changing date/time
        .tz(localTZ, true)
        .toDate()
    }
  </DatePicker>

Playground 版本(链接)

const localDate = moment(new Date()).format();
const { timeZone: localTZ } = Intl.DateTimeFormat().resolvedOptions();
const apiTZ = "utc";
const apiTimeFromPickerLocalDate = moment(localDate)
  .tz(localTZ)
  // change TZ without changing date/time
  .tz(apiTZ, true)
  .format();

const pickerTimeFromApiTime = moment(apiTimeFromPickerLocalDate)
  .tz(apiTZ)
  .tz(localTZ, true)
  .toDate();

console.log("local time", localDate);
console.log("api time", apiTimeFromPickerLocalDate);
console.log("picker time", pickerTimeFromApiTime);
local time 2023-02-09T13:12:39-10:00 
api time 2023-02-09T13:12:39Z 
picker time Thu Feb 09 2023 13:12:39 GMT-1000 (Hawaii-Aleutian Standard Time)

I wanted to just completely disregard the user's local timezone, and not even use it for display or for sending to the API. In order to do that, I used their timezone just to parse dates back and forth from my target api timezone (utc). This is working for me:

it's in momentjs b/c our code is old, can replicate in newer libs

  const { timeZone: localTZ } = Intl.DateTimeFormat().resolvedOptions()
  // The TZ format I want everything to be displayed and submitted as
  const apiTZ = 'utc'
  const [apiTime, setApiTime] = useState(moment.utc())

  <DatePicker
    onChange={date => {
      const apiTimeFromPickerLocalDate = moment(date)
        .tz(localTZ)
        // change TZ without changing date/time
        .tz(apiTZ, true)
        .format()        
      setApiTime(apiTimeFromPickerLocalDate)
    }}
    selected={
      moment(apiTime)
        .tz(apiTZ)
        // Display picker time in local TZ without changing date/time
        .tz(localTZ, true)
        .toDate()
    }
  </DatePicker>

Playground Version (link)

const localDate = moment(new Date()).format();
const { timeZone: localTZ } = Intl.DateTimeFormat().resolvedOptions();
const apiTZ = "utc";
const apiTimeFromPickerLocalDate = moment(localDate)
  .tz(localTZ)
  // change TZ without changing date/time
  .tz(apiTZ, true)
  .format();

const pickerTimeFromApiTime = moment(apiTimeFromPickerLocalDate)
  .tz(apiTZ)
  .tz(localTZ, true)
  .toDate();

console.log("local time", localDate);
console.log("api time", apiTimeFromPickerLocalDate);
console.log("picker time", pickerTimeFromApiTime);
local time 2023-02-09T13:12:39-10:00 
api time 2023-02-09T13:12:39Z 
picker time Thu Feb 09 2023 13:12:39 GMT-1000 (Hawaii-Aleutian Standard Time)
攒眉千度 2025-01-24 08:20:55

这是 react-datepicker 的已知问题

您有 2 种可能的解决方案 -

  1. 您可以在从表单发送数据之前转换日期,
    或者
  2. 您可以将每个时间从 UTC 转换为本地,再从本地转换为 UTC
    用户的日期选择。

您可以在此处查看第二个解决方案的问题和代码实现和<一个href="https://github.com/Hacker0x01/react-datepicker/issues/1787#issuecomment-770313939" rel="nofollow noreferrer">此处

This is known issue for react-datepicker.

You have 2 possible solutions -

  1. You can convert the date before sending data from the form,
    or
  2. You can convert it from UTC to Local and from Local to UTC for each
    date selection of the user.

You can see the the issue and code implementation of the 2nd solution here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文