Syntax problem in a code node

Hello to all,

I have this code that helps me reformatting the dates to match Notions to Hubspot needs.

I get a first error when I run the node:
image

Would you mind having a quick look at it and tell me what is wrong?

Many ythanks in advance!

const inputDate1 = $input.first().json.different['Date EX'];
const inputDate2 = $input.first().json.same['Date CR'];

function formatDate(dateStr) {
  const [day, month, year] = dateStr.split('-');
  return new Date(`${year}-${month}-${day}T00:00:00.000Z`).toISOString();
}

// Formate les deux dates
const hs_expiration_date = formatDate(inputDate1);
const hs_creation_date = formatDate(inputDate2);

// Retourne les dates formatées
return [{
  json: {
    hs_expiration_date,
    hs_creation_date
  }
}];```

## Information on your n8n setup
- **n8n version: 1.94.1
- **Database (default: SQLite):** Postgres
- **n8n EXECUTIONS_PROCESS setting (default: own, main):** 
- **Running n8n via (Docker, npm, n8n cloud, desktop app):** Docker
- **Operating system:** Synology

Hey @Philippe try running this code…

// Safely access date values using optional chaining
const inputDate1 = $input.first().json?.different?.['Date EX'];
const inputDate2 = $input.first().json?.same?.['Date CR'];

// Format function with error handling
function formatDate(dateStr, label) {
  if (!dateStr) {
    throw new Error(`Missing or invalid date string for ${label}: ${dateStr}`);
  }

  const parts = dateStr.split('-');
  if (parts.length !== 3) {
    throw new Error(`Date string format incorrect for ${label}: "${dateStr}"`);
  }

  const [day, month, year] = parts;
  return new Date(`${year}-${month}-${day}T00:00:00.000Z`).toISOString();
}

// Log input values for debugging
console.log('Date EX:', inputDate1);
console.log('Date CR:', inputDate2);

// Format the dates
const hs_expiration_date = formatDate(inputDate1, 'Date EX');
const hs_creation_date = formatDate(inputDate2, 'Date CR');

// Return the formatted results
return [{
  json: {
    hs_expiration_date,
    hs_creation_date
  }
}];

Hey @Sudhanshu_Sharma,
Thanks a lot for your quick reply!

I get this error message now:
image

No problem, @Philippe!

I’d be happy to help further.

Could you please share the sample workflow here along with the pinned data?
That would allow me to replicate the issue on my end and better understand what might be going wrong…it’s a bit difficult to pinpoint the issue without that context.

:bulb:Quick Tip: If you’re using n8n Cloud, try using the AI Assistant by clicking the button shown below the error message:

image

Looking forward to your response!