These are my codes. selectedDate
is a timestamp field:
<ListItemText
primary={
new Date(
user.items?.selectedDate?.seconds * 1000
).toDateString()
" at "
new Date(
user.items?.selectedDate?.seconds * 1000
).toLocaleTimeString()
}
/>
It does show the data correctly. However, if the selectedDate
does exist, it will show:
Invalid date at Invalid date
How can I fix this?
CodePudding user response:
As Doug commented, you'll want to check whether the document has a `` field, before trying to convert its value to a date. Something like this should do the trick:
<ListItemText
primary={user.items?.selectedDate ? (
new Date(
user.items?.selectedDate?.seconds * 1000
).toDateString()
" at "
new Date(
user.items?.selectedDate?.seconds * 1000
).toLocaleTimeString()
) : "No date" }
/>