简体   繁体   中英

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

Just use an array map to convert it.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM