React格式日期无效的时间值



我有下面的代码,我转换startDate.

import React from "react";
import type { RoomBooking } from "../../services/room/room";
import format from "date-fns/format";

export const BookingList = ({ bookings }: { bookings: RoomBooking[] }) => (
<ul>
{bookings.map((booking, index) => (
<li key={`booking-${index}`}>
{format(new Date(booking.startDate), "dd/MM/yyyy")}
</li>
))}
</ul>
);

有了这个代码,我有这个错误RangeError: Invalid time value,当我记录bookings列表时,我发现它是空的,所以我怎么能检查预订是否不为空?

如果你想提供一个默认日期,你可以这样检查

{format(new Date(booking?.startDate || ""), "dd/MM/yyyy")}

更好的可能是

{
booking?.startDate ?
format(new Date(booking.startDate), "dd/MM/yyyy") :
"No date"
}

相关内容

  • 没有找到相关文章

最新更新