JS date editing

I'm trying to take a date from an input and add a certain number of days. I can grab the input value but trying to add a number of days causes it to silently crash. My research into how to accomplish this show a lot of people using the code exactly as I have, but it's difficult to know what's going wrong due to lack of errors.

Running this script results in:

image

The date is being created (console log 1), the date is being updated to the user defined input (console log 2) but then it just stops.
Any insight would be very much appreciated.

Thanks

Hi there @LucasT,

The issue is that your newTicket_DateInput2.value string is in "YYYY-MM-DD" format, which means when you create a Date object from it, JavaScript will parse it as UTC, not local time, potentially causing unexpected results with date math.

To safely add 14 days to testDate, you can use something like this:

const testDate = newTicket_DateInput2.value
const dateObj = new Date(testDate + "T00:00:00"); // force consistent parsing
dateObj.setDate(dateObj.getDate() + 14);

const newDate = dateObj.toISOString().slice(0, 10); // returns in "YYYY-MM-DD"
console.log(newDate);

return newDate

Hope this helps!

1 Like

That did help tremendously, thank you so much! That's been blocking progress for longer than I'd like to admit :upside_down_face::+1:

Tell me about it, dates are very tricky and I've also wasted a considerable amount of time in the past dealing with them!

Glad I was able to help