I'm trying to convert a date returned to me as a string (format: "2020-06-15T12:41:16+00:00") into a date object.
So I get my list of items in a fetchResults() function, and I thought I could do the conversion at the time of the .map in the return, only I don't really see the syntax I could use...
Anyone have an idea?
My component react :
const ResultList = ({userId}) => {
const [resultsList, setResultList] = useState({})
const [loading, setLoading] = useState(true)
useEffect(() => {
fetchResults()
}, [userId])
const fetchResults = async () => {
try {
const data = await resultsAPI.findResultsByUser(userId)
setResultList(data)
setLoading(false)
} catch (error) {
console.log(error)
}
}
return(
<>
{!loading &&
<>
<h3 className="resultCount">You have {resultsList.length} results</h3>
<ul className="resultsList">
{resultsList.map(result => {
return(
<li key={result.id} className="resultItem">
<span>{result.resultDate}</span> //HERE CONVERT STRING TO DATE
<span>{result.Maturity}</span>
</li>
)
})}
</ul>
</>
}
</>
)
}
export default ResultList
Thanks !
new Date(result.resultDate)? However this will not help you much as Date object will be converted to string again for display purpose. So the question is, what are you trying to achieve?Date-object anyways. Is there any particular reason why you need to do that? Do you need to convert your date string to some other format?