I am looking for a good clean solution for this. I have an object with dates that can be type Date | null | string:
someObject: {
array: [
{
dateOne: '2021-10-21T22:00:00.000Z',
dateTwo: '2021-10-21T22:00:00.000Z',
}
];
};
I am looking for a good solution to apply date formatting to the above without changing its structure. I am aware that the below example would not work, its just an example that would work for nested Dates in an object, but maybe something along these lines. Example below:
someObject: Object.entries(values.someObject).reduce((acc, [a, b]) => {
if (b instanceof Date) {
return {
...acc,
[a]: moment(b).format('YYYY-MM-DD'),
};
}
return acc;
}, values.someObject),
Expected output:
someObject: {
array: [
{
dateOne: '2021-10-21',
dateTwo: '2021-10-21',
}
];
};
TIA