[英]Change date format of DatePicker in React
I am just trying to display a date from the DatePicker component in React but it displays it as an Object:我只是想在 React 中显示 DatePicker 组件的日期,但它显示为 Object:
startDateOwner: Mon Feb 08 2021 00:00:00 GMT+0100 (Central European Standard Time)
How could I display it as 08/02/2021 and as a string?如何将其显示为 08/02/2021 和字符串?
Here is the code I am using:这是我正在使用的代码:
const Calendar = ({ startDate, endDate, handleChangeDates }) => ( <div> <label> Beginning date <DatePicker name = "startDate" selected={startDate} selectsStart startDate={startDate} endDate={endDate} onChange={(date)=>handleChangeDates(date, "startDate")} /> </label> <label> End date <DatePicker name = "endDate" selected={endDate} selectsEnd startDate={startDate} endDate={endDate} minDate={startDate} onChange={(date)=>handleChangeDates(date, "endDate")} /> </label> </div>
Thank you !谢谢 ! Julie
朱丽叶
React date picker provides a attribute dateFormat
React 日期选择器提供了一个属性
dateFormat
<DatePicker
name = "startDate"
selected={startDate}
selectsStart
startDate={startDate}
endDate={endDate}
onChange={(date)=>handleChangeDates(formatDateFn(date), "startDate")}
dateFormat="dd/MM/YYYY" (Use dd/MM/yyyy if you get error in console)
/>
const formatDateFn = (date) => {
const selectedDate = new Date(date)
return selectedDate.getDate() + "/"+ parseInt(selectedDate.getMonth()+1) +"/"+ selectedDate.getFullYear();
}
You are welcome!不客气! Subodh
苏博德
If you're objective is to just display the date in (dd/mm/yyyy) format, you may use moment如果您的目标是仅以 (dd/mm/yyyy) 格式显示日期,则可以使用moment
moment(date_variable).format("DD/MM/YYYY")
This would work to display date.这将用于显示日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.