I have here a piece of code getIsoDate that works exactly as I want. But later I need to pass it further as one single argument instead of three arguments like I have now (day, month, year). Thats why I get the error - Expected 3 arguments, but got 1.ts(2554)
So in order to get rid of the error I need to rebuild getIsoDate that it has one argument but the same function and I am struggling to understand how to achieve that, get rid of day, month, year and use only one value? Or is there another approach? Can you please give me a hint or tip how to start?
const getIsoDate = (day: string, month: string, year: string) => {
if (day && year && month && parseInt(year) > 999)
return new Date(`${year}-${month}-${day}T12:00:00`).toISOString();
else if (!day && !month && (!year || parseInt(year) < 1000)) return undefined;
else return "Invalid Date";
You can destructure the arguments like so if you pass in an object with the corresponding keys:
const getIsoDate = ({ day, month, year}) => {
if (day && year && month && parseInt(year) > 999)
return new Date(`${year}-${month}-${day}T12:00:00`).toISOString();
else if (!day && !month && (!year || parseInt(year) < 1000)) return undefined;
else return "Invalid Date";
}
// exmaple usage
const someDate = { day: '25', month: '04', year: '2022' }
console.log(getIsoDate(someDate))