Click here to Skip to main content
15,891,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a nested object in an array like:
JavaScript
[{amount: 73, date: '1605015361'},
{amount: 115, date: '1605018961'},
{amount: 135, date: '1605022560'},
{amount: 75, date: '1605026160'},]

And I'm trying to figure out how I can transform it into regular date like:
JavaScript
[{amount: 73, date: '11/10/2020'},
{amount: 115, date: '11/10/2020'},
{amount: 135, date: '11/10/2020'},
{amount: 75, date: '11/10/2020'}]


Thanks so much!

What I have tried:

I know I can convert the unix date using:
JavaScript
const milliseconds = unixDate * 1000 
              
const dateObject = new Date(milliseconds)
              
const regularDate = dateObject.toLocaleString()

but I'm not sure how to go through the array of objects and save the new date format inside, while preserving the original structure.
Posted
Updated 13-Nov-20 12:22pm

1 solution

JavaScript
var ar1 = [
    { amount: 73, date: '1605015361' },
    { amount: 115, date: '1605018961' },
    { amount: 135, date: '1605022560' },
    { amount: 75, date: '1605026160' }
];

function converter(array) {
    for (let x = 0; x < array.length; x++) {
        var neww = new Date(array[x].date * 1000).toLocaleDateString("en-US")
        console.log(neww);
        array[x].date = neww
    }
}
converter(ar1);
console.log(ar1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900