David
David

Reputation: 39

How to turn an array of miliseconds into date format mm-dd-yyyy

i have the next array of dates in miliseconds

const dates = date?.rangeOfDates
console.log(dates)
(4) [1661281243730, 1661454043730, 1661886043730, 1661713243730]

Im trying to turn it into Date format the next way:

const listOfDates = new Date(dates).toLocaleDateString()

It gives me an Invalid Date error in the console but when I try to change it manually in the next way it works good:

console.log(new Date(1661281243730).toLocaleDateString())
--> 8/23/2022

Upvotes: 0

Views: 36

Answers (2)

Arcord
Arcord

Reputation: 1909

"new Date" will try to create a single date from a single value and "dates" is not a single value but an array.

Try this :

realDates = dates.map(d => new Date(d))

For each value in "dates" it will convert it to a date and you'll get "realDates" array.

Upvotes: 1

C. Helling
C. Helling

Reputation: 1402

Just use an array map to convert it.

const array = [1661281243730, 1661454043730, 1661886043730, 1661713243730];
array.map(x => new Date(x).toLocaleDateString());

Upvotes: 1

Related Questions