Help

Re: Adding days to a date and outputting month day and year only

1671 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Jen_Rudd
7 - App Architect
7 - App Architect

Memorializing this since I spent entirely too much time and bugged @Justin_Barrett @Bill.French and @openside to get this output. I needed to output the US date format into an email, but I needed “tomorrow’s date” to be output. The use case was an email that goes out through automations every Wednesday, but they needed a reminder in the email to follow up by Thursday, (Tomorrow’s Date).

Here’s the outcome. It works, so we don’t try to make it more elegant in these parts :slightly_smiling_face:

let someDate = new Date();
someDate.setDate(someDate.getDate() + 1); 
let dateFormatted = someDate.toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit'});
output.set('thisDate', dateFormatted);

You can add whatever number to the second line to add, haven’t tried subtracting. Also, the toLocaleString() without any modifiers outputs a date and time.

2 Replies 2

It will work. the method getDate() is to days what getHour() is to hours. As such, if you needed yesterday’s date:

someDate.setDate(someDate.getDate() - 1);

… or thee days prior:

someDate.setDate(someDate.getDate() - 3);

… the next hour:

someDate.setDate(someDate.getHours() + 1);

As to passing the output to subsequent automation steps, output.set() is really handy. Ideally, though, I tend to attempt to self document the code by stating the nature of the passed result data:

output.set('tomorrowsDate', dateFormatted);

In case anyone reading is new to JavaScript and is wondering if changing a day as described above only changes the day, that’s not necessarily the case. Similar to how Airtable’s DATEADD() function knows to adjust the month if the right number of days are added/subtracted from a start date, the various set methods on Date objects do the same thing. In fact, the reason that the DATEADD() function is so “smart” is because it’s doing all of the calculations in JavaScript behind the scenes. It’s really the Date object that’s responsible for all of that date-adjusting intelligence.