反应日期选择器中的日期格式不起作用

发布于 2025-01-10 05:52:50 字数 515 浏览 5 评论 0原文

我希望获取格式为 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 技术交流群。

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

发布评论

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

评论(1

捂风挽笑 2025-01-17 05:52:50

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.

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