Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change date format in reactjs

I am working with reactjs and i am using "nextjs" framework, I am fetching "blogs data" using loop, but problem is "i want to change date format of blogs"
Here is my current code

{user && user.length > 0 && user.map((userObj, index) => (
                      <NewsCard
                    key={index}
                    img={userObj.image}
                    title={userObj.title}
                    date={userObj.CreatedAt}   // getting date for example "2023-03-26 01:40:26"
        />
                ))}

My expected date format is for example
April 29,2023
How can i do this ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the toLocaleDateString() method to convert the date

{user && user.length > 0 && user.map((userObj, index) => (
    <NewsCard
        key={index}
        img={userObj.image}
        title={userObj.title}
        date={new Date(userObj.CreatedAt).toLocaleDateString('en-US', {
            month: 'long',
            day: 'numeric',
            year: 'numeric',
        })}
    />
))}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading