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 ?
>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',
})}
/>
))}