Let's say I have an array of objects:
[
{
"id": 2,
"Date": "2021-12-16T13:55:00.000Z"
},
{
"id": 9,
"Date": "2021-12-16T13:55:00.000Z"
},
{
"id": 14,
"Date": "2021-12-16T13:55:00.000Z"
}
]
It seem Like This form
[
{
"id": 2,
"Date": "2021-12-16 07:25 PM"
},
{
"id": 9,
"Date": "2021-12-16 07:25 PM"
},
{
"id": 14,
"Date": "2021-12-16T13:55:00.000Z"
}
]
please Help me
CodePudding user response:
I hope that I understood your question correctly..
The following Code will edit every "Date" element and convert it.
var arr = [
{
"id": 2,
"Date": "2021-12-16T13:55:00.000Z"
},
{
"id": 9,
"Date": "2021-12-16T13:55:00.000Z"
},
{
"id": 14,
"Date": "2021-12-16T13:55:00.000Z"
}
];
arr.forEach((element, index) => {
var date = new Date(element.Date)
var newdate = date.toISOString().slice(0, 10) " " date.toTimeString().slice(0,5) " " (date.getHours() >= 12 ? "PM" : "AM");
arr[index].Date = newdate;
});
console.log(arr);
CodePudding user response:
You can do it like so:
const data = [{
id: 2,
Date: '2021-12-16T13:55:00.000Z'
},
{
id: 9,
Date: '2021-12-16T13:55:00.000Z'
},
{
id: 14,
Date: '2021-12-16T13:55:00.000Z'
}
];
const newData = data.map((el => ({
...el,
Date: (new Date(Date.parse(el.Date))).toLocaleString('en-US')
})));
console.log(newData);