反应日期选择器中的日期格式不起作用
我希望获取格式为 yyyy/MM/dd
的日期 值时,它似乎不起作用,
<DatePicker dateFormat='dd/MM/yyyy' selected={startDate} onChange={(date)=>{
setStartDate(date)
console.log(startDate)
}}/>
但当我 console.log
得到以下格式的
Fri Dec 31 2021 18:10:31 GMT+0530 (India Standard Time)
我希望以 yyyy-mm-dd
的形式最终插入日期在 sql 表中,
我可以在日期选择器组件中看到该格式,但是当 console.log 时,它会显示不同的格式,或者即使可以像 e.target.value 一样直接获取显示的值,它也会很有用
i wish to get the date in the format yyyy/MM/dd
but it doesn't seem to work when i console.log
the value
<DatePicker dateFormat='dd/MM/yyyy' selected={startDate} onChange={(date)=>{
setStartDate(date)
console.log(startDate)
}}/>
i get the following format
Fri Dec 31 2021 18:10:31 GMT+0530 (India Standard Time)
i will want in the form yyyy-mm-dd
to finally insert the date into a sql table
i can see that format in the date-picker component but when console.log then it shows a different format or it will be useful even if it possible to get the value shown directly like e.target.value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dateFormat 属性控制日期在日期选择器旁边的输入字段中的显示方式。
日期选择器的实际值仍然是 Date 对象。
有多种方法可以仅获取此日期的日期(以特定格式)。
我建议在将其发送到 api/服务器之前应用其中之一,例如 (.toLocaleDateString())。
请记住,.toLocaleDateString() 还允许您传递区域设置和/或任何选项,例如时区或您可能想要指定的格式。请参阅此处的文档了解更多信息例子。
The dateFormat prop controls how the date is displayed in the input field next to the date picker.
The actual value of the date picker will still be a Date object.
There are various ways of getting only the date (in a specific format) form this date.
I would suggest applying one of these for example (.toLocaleDateString()) before you send it to your api/server.
Keep in mind that .toLocaleDateString() also allows you to pass the locales, and or any options like a timezone, or a format which you might want to specify. See the documentation here for more examples.