How can we write a handlebar to set first of month in start date and end of the month in last date whenever the flow runs in celigo in export request body

How can we write a handlebar to set first of month in start date and end of the month in last date whenever the flow runs in celigo in export request body

You can set the start of the month using this Handlebars expression:

{{dateFormat 'YYYY-MM-01' (timestamp)}}

That works fine and gives you something like "2025-05-01".

For the end of the month, it’s a bit more complex. You might try this:

{{dateFormat 'YYYY-MM-DD' (dateAdd (timestamp) '1M -1d')}}

But that doesn’t work in Celigo because dateAdd only supports offsets in milliseconds, not strings like '1M' or '-1d'. It also doesn’t support chaining or nesting helpers.

Even this:

{
  "startDate": "{{dateFormat 'YYYY-MM-01' (timestamp)}}",
  "endDate": "{{dateFormat 'YYYY-MM-DD' (dateAdd (dateFormat 'YYYY-MM-01' (timestamp)) '1M' '-1d' 'UTC')}}"
}

...ends up giving something like:

{
  "startDate": "2025-05-01",
  "endDate": "2025-05-15"
}

That’s just today’s date, not the last day of the month. So the only reliable way to get the true last day of the current month is with JavaScript in a pre-map hook.

Here’s what you can use in the script:

var now = new Date();
var start = new Date(now.getFullYear(), now.getMonth(), 1);
var end = new Date(now.getFullYear(), now.getMonth() + 1, 0);

function formatDate(date) {
  return date.toISOString().split("T")[0];
}

export.startDate = formatDate(start);
export.endDate = formatDate(end);

Then in your export request body, just use:

"startDate": "{{startDate}}",
"endDate": "{{endDate}}"

This will always give you the correct first and last day of the month whenever the flow runs.

how can I can write this script in export step.

You can check it here:
https://docs.celigo.com/hc/en-us/articles/360007562931-Create-hook-scripts-for-exports-and-imports

Here is another post where I made a handlebar to get last day of the month!

2 Likes

Script route wouldn't work in this case since there is no pre-request script type (like postman) on exports.