I have an app that is sending a date in this format:
inputDate: “February 7, 2025”
inputFormat: “MMMM DD YYYY”
However the date formatter throws an error every time the day or month number is not Zero padded 07 instead of 7.
There is no way to edit the source data, and I can’t figure out to use Routers to splice in the Zero when needed. This feels like it should just work…and not need a bunch of manual work around. Am I missing something simple?
Unfortunately, I couldn’t find a way to resolve this with the basic date formatter step. However, you can easily fix this by replacing the date formatter step with a code step.
If you use inputDate as an input variable and apply the code below, the output should be exactly as you need it:
export const code = async (inputs) => {
// Extract the input date as a string in the format "MMMM (D)D, YYYY"
const inputDate = inputs.inputDate; // e.g., "February 7, 2025"
// Create a map to convert month names to their respective numbers
const monthMap = {
"January": "01",
"February": "02",
"March": "03",
"April": "04",
"May": "05",
"June": "06",
"July": "07",
"August": "08",
"September": "09",
"October": "10",
"November": "11",
"December": "12"
};
// Use regular expression to parse the date components
const dateRegex = /(\w+)\s+(\d{1,2}),\s+(\d{4})/;
const match = dateRegex.exec(inputDate);
if (!match) {
throw new Error("Invalid date format");
}
const monthName = match[1];
const day = match[2];
const year = match[3];
// Convert to zero-padded strings as needed
const month = monthMap[monthName];
const dayPadded = String(day).padStart(2, '0');
// Construct the final date in "YYYY-MM-DD"
const formattedDate = `${year}-${month}-${dayPadded}`; // "2025-02-07"
return formattedDate;
};```
Let me know if you've any questions!