Custom Javascript adding months to datetime incorrectly
I’ve been trying to add a custom action to a Zap (and then to do the same with custom code) and seem to be having an issue with dates/datetimes. I am simply attempting to add 12 months to a specific date in Javascript. My code works in *every other interface* that I test it in, but for some reason, the same code returns inaccurate outputs when I use Zapier custom code or custom actions. Code is here:
const numMonths = 12; const unixStartDate = 1743179322; //March 28th, 2025 in unix - set dynamically in the eventual action
const startDate = new Date(unixStartDate * 1000); //Convert unix to datetime console.log('Start Date ::: '+startDate);
const newEndDate = new Date(startDate.setMonth(startDate.getMonth() + numMonths)); //Add 12 months console.log('End Date ::: '+newEndDate); //Would expect March 28th, 2026
This code runs correctly on other interfaces, but when I run it in Zapier, it returns the date Fri Sep 19 2042 -- 18 years ahead. I can find no other reasonable solution other than something is wrong on Zapier's side. Any help would be appreciated.
Page 1 / 1
Hi @zgoldberg Why not use this Zap action: Formatter > Date/Time > Add/Subtract Time
TIP: Try asking ChatGPT to help you troubleshoot and optimize the Code.
Thanks for the tip about the formatter, @Troy Tessalone, that could be a temporary solution. However, now I’m using 3 actions (transform the Unix code to a datetime > add the months > convert back to Unix) where I should be able to do this all in a single code block.
Again, the code - as far as I can tell - is not the issue. I have successfully used it in a number of other interfaces. I’m wondering why it’s only causing this issue in Zapier and not elsewhere.
@zgoldberg
NOTES:
Formatter steps count as 0 Tasks in Zap Runs
Codes steps count as 1 Task in Zap Runs
With the help of ChatGPT I got this to JavaScript Code to work:
We just wanted to see how everything is going with your Zap. Did Troy's recommendation get the job done? Feel free to reach out if you need further assistance with your Zap. We're glad to address any concerns and assist you.
We're looking forward to your response.
Thanks so much @Troy Tessalone that does work, though honestly I’m not sure why. I have the following code in a custom action:
// Calculate the new end date using DateTime values const startDate = new Date(subscription.current_period_start * 1000); // Convert Unix timestamp to milliseconds console.log('StartDate in Unix ::: '+subscription.current_period_start); console.log('MONTHS ::: '+months); console.log('FORMATTED START DATE ::: '+startDate); const newEndDate = new Date(startDate); newEndDate.setMonth(newEndDate.getMonth()+months); console.log('OG VALUE OF NEW ENDDATE ::: '+newEndDate);
// Convert the new end date to a Unix timestamp const newEndDateUnix = Math.floor(newEndDate.getTime() / 1000); console.log('EndDate in Unix ::: '+newEndDateUnix);
And I end up with the following log:
StartDate in Unix ::: 1743194789
MONTHS ::: 12
FORMATTED START DATE ::: Fri Mar 28 2025 20:46:29 GMT+0000 (Coordinated Universal Time)
OG VALUE OF NEW ENDDATE ::: Sun Sep 28 2042 20:46:29 GMT+0000 (Coordinated Universal Time)
EndDate in Unix ::: 2295549989
I can’t for the life of my understand why it’s calculating this way. Again- this is only on the custom action. Using a Code by Zapier action, it seems to be fine. Not sure if I’m still missing something or if something is wrong with the action itself. Regardless, I appreciate it!
Thank you for confirming that Troy’s resolution got the Zap running. This will significantly help our Community members to have as a reference for the same issue.