在react.js中获取onChange事件的准确值
我正在尝试获取日期 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Flatpickr 的
onChange
属性的第一个参数是selectedDates
- 日期数组(您可以使用selectedDates[0]
访问第一个参数并格式化它就像对待任何日期一样)。第二个是
dateStr
- 由dateFormat
选项格式化。Flatpickr 文档
The 1st param of Flatpickr's
onChange
prop isselectedDates
- an array of Dates (you can just access the first withselectedDates[0]
and format it like you would with any Date).The 2nd is the
dateStr
- as formatted by thedateFormat
option.Flatpickr docs
您可以打印日期,例如
单独获取日期字符串的代码:
Codesandbox:
You can print the date like,
Code to fetch the date string alone:
Codesandbox:
那么您的数组只有一个元素,因此您可以使用下面的代码。
它将为您提供所需格式的数据。
Well your array only has one element so you can use the code below.
It will give you the data in the format you need.