0

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 !

5
  • 2
    Did you try 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? Commented Jun 15, 2020 at 13:39
  • 3
    You will not be able to render 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? Commented Jun 15, 2020 at 13:40
  • new Date(result.resultDate.split("T")[0]), will this work for you? Commented Jun 15, 2020 at 13:51
  • @YevgenGorbunkov I understand that, yes... I'd need to get the day in a thong, finally get a date in text form... But I think I'll settle for that. Commented Jun 15, 2020 at 14:15
  • @Rajesh Date(result.resultDate) to convert to date format in the display . All you had to do was remove the new... I'll try to convert it before putting it in the state, so I can getDay() Commented Jun 15, 2020 at 14:18

2 Answers 2

1

Use moment.js. You can able to convert a date string to any desired date format. For that you need to know the current date format of a date string

import moment from "moment";    
let resultDate = '2020-06-15 17:44:18'; // which is YYYY-MM-DD hh:mm:ss

<span>{moment(resultDate, 'YYYY-MM-DD hh:mm:ss').format('MM-DD-YYYY')}</span> // It will return 06-15-2020
Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't thought of "moment"... Thank you!
0
<li key={result.id} className="resultItem">
 <span> {new Date(`${result.resultDate}`).toDateString()}</span> // use template literals
  <span>{result.Maturity}</span>

Use this will defenitly work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.