I have an array of objects structured like this:
const days = [
{
_id: 12312323,
date : '30/12/2021',
dateStatus : 'presence'
},
...
]
I am looking to convert the date property from a string to a Date object using the following method:
const convertToDateObject = (dateString: string): Date => {
const dateParts: any[] = dateString.split("/");
return new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
};
for (let i = 0; i < days.length; i++) {
const d = days[i].date as string;
days[i].date = convertToDateObject(d);
}
However, the conversion results in a format similar to
'Wed Dec 01 2021 00:00:00 GMT+0100 (Central European Standard Time)'
, rather than the desired format at 2021-11-30T23:00:00.000Z
.
I am confused because when the object does not contain the _id
property, the formatting works correctly and provides the expected output, but if it includes the _id
property, it gives me a different format. Why is this happening?