在react.js中获取onChange事件的准确值

发布于 2025-01-11 12:20:07 字数 610 浏览 5 评论 0原文

我正在尝试获取日期 onChange 的值:

<Flatpickr
   data-enable-time
   name="goodsreadyby"
     options={{
       minDate: date,
       enableTime: true,
       dateFormat:
        'Y-m-d H:i:s'
       }}
     onChange={(date) => {setGoodsReadyBy({date})
     }}/>

我的问题是我收到了数组 onChange 事件。 目前:

{date: Array(1)}
date: Array(1)
0: Sat Mar 05 2022 16:20:00  (Some Standard Time)
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
[[Prototype]]: Object

但我只需要 Sat Mar 05 2022 16:20:00 (Some Standard Time) 这样的值。我如何修改我的代码以获得我需要的确切值。

I'm trying to get the value of the date onChange:

<Flatpickr
   data-enable-time
   name="goodsreadyby"
     options={{
       minDate: date,
       enableTime: true,
       dateFormat:
        'Y-m-d H:i:s'
       }}
     onChange={(date) => {setGoodsReadyBy({date})
     }}/>

My problem is that I'm getting Array onChange event.
Currently :

{date: Array(1)}
date: Array(1)
0: Sat Mar 05 2022 16:20:00  (Some Standard Time)
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
[[Prototype]]: Object

But I need the value like Sat Mar 05 2022 16:20:00 (Some Standard Time) only. How did I modify my code to get the exact value that I needed.

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

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

发布评论

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

评论(3

您的好友蓝忘机已上羡 2025-01-18 12:20:07

Flatpickr 的 onChange 属性的第一个参数是 selectedDates - 日期数组(您可以使用 selectedDates[0] 访问第一个参数并格式化它就像对待任何日期一样)。

第二个是 dateStr - 由 dateFormat 选项格式化。

export default function App() {
  return (
    <Flatpickr
      data-enable-time
      name="goodsreadyby"
      options={{
        enableTime: true,
        dateFormat: "Y-m-d H:i:s"
      }}
      onChange={(selectedDates, dateStr, instance) => {
        const firstDate = selectedDates[0];
        console.log({ firstDate, dateStr });
      }}
    />
  );
}

onChange 调用时将接收 3 个参数。这些是:

  • selectedDates - 用户选择的日期对象数组。当没有选择日期时,数组为空。
  • dateStr - 用户最新选择的 Date 对象的字符串表示形式。该字符串按照 dateFormat 选项进行格式化。
  • instance - flatpickr 对象,包含各种方法和属性。

Flatpickr 文档

编辑 Late-brook-45k242

The 1st param of Flatpickr's onChange prop is selectedDates - an array of Dates (you can just access the first with selectedDates[0] and format it like you would with any Date).

The 2nd is the dateStr - as formatted by the dateFormat option.

export default function App() {
  return (
    <Flatpickr
      data-enable-time
      name="goodsreadyby"
      options={{
        enableTime: true,
        dateFormat: "Y-m-d H:i:s"
      }}
      onChange={(selectedDates, dateStr, instance) => {
        const firstDate = selectedDates[0];
        console.log({ firstDate, dateStr });
      }}
    />
  );
}

onChange will receive 3 arguments when called. These are:

  • selectedDates - an array of Date objects selected by the user. When there are no dates selected, the array is empty.
  • dateStr - a string representation of the latest selected Date object by the user. The string is formatted as per the dateFormat option.
  • instance - the flatpickr object, containing various methods and properties.

Flatpickr docs

Edit late-brook-45k242

南城追梦 2025-01-18 12:20:07

您可以打印日期,例如

goodsReadyBy?.date[0].toString()

单独获取日期字符串的代码:

const App = () => {
  const [goodsReadyBy, setGoodsReadyBy] = useState();
  return (
    <div>
      <Flatpickr
        data-enable-time
        name="goodsreadyby"
        options={{
          enableTime: true,
          dateFormat: "Y-m-d H:i:s"
        }}
        onChange={(date) => {
          setGoodsReadyBy({ date });
        }}
      />
      <p> {goodsReadyBy?.date[0].toString()} </p>
    </div>
  );
};

Codesandbox:

编辑示例 - React Hooks - useState (forked)

You can print the date like,

goodsReadyBy?.date[0].toString()

Code to fetch the date string alone:

const App = () => {
  const [goodsReadyBy, setGoodsReadyBy] = useState();
  return (
    <div>
      <Flatpickr
        data-enable-time
        name="goodsreadyby"
        options={{
          enableTime: true,
          dateFormat: "Y-m-d H:i:s"
        }}
        onChange={(date) => {
          setGoodsReadyBy({ date });
        }}
      />
      <p> {goodsReadyBy?.date[0].toString()} </p>
    </div>
  );
};

Codesandbox:

Edit Example - React Hooks - useState (forked)

花海 2025-01-18 12:20:07

那么您的数组只有一个元素,因此您可以使用下面的代码。

 onChange={(date) => {setGoodsReadyBy({date[0]})

它将为您提供所需格式的数据。

Well your array only has one element so you can use the code below.

 onChange={(date) => {setGoodsReadyBy({date[0]})

It will give you the data in the format you need.

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