I’m having trouble with a date formatting function that works fine in my local development environment but throws an error when I run it in Zapier. The error message says TypeError: dateobj.getFullYear is not a function, which suggests the input isn’t being recognized as a proper Date object.
Here’s my current code:
function formatDateTime(inputDate, pattern) {
var yr = inputDate.getFullYear();
var mon = ("0" + (inputDate.getMonth() + 1)).slice(-2);
var day = ("0" + inputDate.getDate()).slice(-2);
var hr = ("0" + inputDate.getHours()).slice(-2);
var min = ("0" + inputDate.getMinutes()).slice(-2);
var sec = ("0" + inputDate.getSeconds()).slice(-2);
var dayIndex = inputDate.getDay();
var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var dayNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
var result = "";
switch(pattern) {
case "YYYY-MM-DD":
result = yr + "-" + mon + "-" + day;
break;
case "YYYY-MMM-DD DDD":
result = yr + "-" + monthNames[parseInt(mon) - 1] + "-" + day + " " + dayNames[parseInt(dayIndex)];
break;
}
return result;
}
var dateInput = input.VIP_2bParsed;
var formatPattern = "YYYY-MMM-DD DDD";
var formattedResult = formatDateTime(dateInput, formatPattern);
output = {formattedResult: formattedResult};
What could be causing this issue in Zapier specifically? Do I need to convert the input to a Date object first?