[英]How to reformat react calendar date
So I am currently creating a website for receptionists and would like the relevant appointments to appear when a date is clicked on react calendar.因此,我目前正在为接待员创建一个网站,并希望在单击反应日历上的日期时显示相关约会。
Currently, when I console.log the clicked date it appears in the following format: Tue Jun 07 2022 00:00:00 GMT+0200 (South Africa Standard Time)目前,当我 console.log 单击日期时,它以以下格式显示:Tue Jun 07 2022 00:00:00 GMT+0200(南非标准时间)
I would like it to appear instead as: 2022-06-07我希望它改为:2022-06-07
This is so that it is coherent with my database.这是为了与我的数据库保持一致。
My code is as follows:我的代码如下:
const Calendar = () => {
const CalendarChange = () => {
console.log("hi");
};
const [date, setDate] = useState(new Date());
const onDateChange = (newDate) => {
// let NewDate = new Intl.DateTimeFormat('en-US').format(date);
setDate(newDate);
console.log(newDate);
}
const locale = 'fr-CA';
return (
<>
<CalendarContainer>
<Calendarcom calendarType='US' format="dd-MM-yyyy" className='react-calendar'
onChange={onDateChange}
value={date} />
</CalendarContainer>
</>
);
};
export default Calendar;
Thank you for everyone's help!谢谢大家的帮助!
I managed to get it working with the following code:我设法让它使用以下代码:
const [date, setDate] = useState(new Date());
const [formatDate, setFormatDate] = useState();
// console.log(new Intl.DateTimeFormat('en-US').format(date));
const onDateChange = (newDate) => {
setDate(newDate);
}
useEffect(() => {
console.log(date);
setFormatDate(new Intl.DateTimeFormat('en-US').format(date));
console.log(formatDate);
}, [date, onDateChange])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.