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

Time:08-17

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

CodePudding user response:

Just use an array map to convert it.

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

CodePudding user response:

"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.

  • Related