我有下面的代码,我转换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"
}