I have the following array of dates. How can I format each date in this array to ‘mm/dd/yyyy’?
I am attempting something like this which isn’t formatting correctly.
const formattedDates = this.displayEffectiveDate.map(date => new Date(date));
[ "2024-02-29T00:00:00", "2024-03-31T00:00:00" ]
5
Answers
You can use
toLocaleDateString()
to get the output for your advantage.Refer the code below:
You can use the
Date
object, then use method for this like this:It seems to me that this is just a matter of string manipulation for which you don’t need to create Date objects:
Try this
If your
displayEffectiveDate
looks like this:this.displayEffectiveDate = ["2024-02-29T00:00:00", "2024-03-31T00:00:00"];
You could create a new
Date
object from the ISO 8601 date string.d.getMonth() + 1
to get the monthd.getDate()
for the dayd.getFullYear()
for the yearString(...).padStart(2, '0')
to ensure the month and day are always two digitsIt should look like this:
The result would be
[ '02/29/2024', '03/31/2024' ]