Help

Re: ReferenceError: DATEADD is not defined

Solved
Jump to Solution
1218 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Valdemar_T
6 - Interface Innovator
6 - Interface Innovator

Trying to add indicated number of days to date via scripting. But the system gives an error. Anyone has any clues about what is wrong? Maybe the DATEADD function is available to formulas only and is not available for scripting?

ReferenceError: DATEADD is not defined

Code:

let current_date = new Date();
let date_added = DATEADD(current_date, 7, 'days') 
1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

Correct. All of the functions listed in the formula field reference are only usable in formulas.

To add a certain number of days to a given Date instance, you need to use the .setDate() method on the instance. This is most often done in conjunction with the .getDate() method to retrieve the existing date, like so:

let current_date = new Date();
let date_added = current_date.setDate(currentDate.getDate() + 7);

If the new date goes past the highest date in the month, JavaScript automatically adjusts the month appropriately. For example, if current_date were today (Sept 28th), the .getDate() method would return 28. Adding 7 to that would give you 35, but because there aren’t 35 days in September, JavaScript knows to increase the month and adjust the date appropriately, setting the new date to Oct 5th.

If you’re new to JavaScript, I strongly recommend keeping a language reference handy. It may not always be easy to know what you want to do if you’re trying to find the equivalent to a formula field function, so feel free to ask if you need guidance.

See Solution in Thread

5 Replies 5
Justin_Barrett
18 - Pluto
18 - Pluto

Correct. All of the functions listed in the formula field reference are only usable in formulas.

To add a certain number of days to a given Date instance, you need to use the .setDate() method on the instance. This is most often done in conjunction with the .getDate() method to retrieve the existing date, like so:

let current_date = new Date();
let date_added = current_date.setDate(currentDate.getDate() + 7);

If the new date goes past the highest date in the month, JavaScript automatically adjusts the month appropriately. For example, if current_date were today (Sept 28th), the .getDate() method would return 28. Adding 7 to that would give you 35, but because there aren’t 35 days in September, JavaScript knows to increase the month and adjust the date appropriately, setting the new date to Oct 5th.

If you’re new to JavaScript, I strongly recommend keeping a language reference handy. It may not always be easy to know what you want to do if you’re trying to find the equivalent to a formula field function, so feel free to ask if you need guidance.

Thanks for a solution indeed it is working perfectly, with a small change when current date is not added to a new variable!

let current_date = new Date();
current_date.setDate(currentDate.getDate() + 7);

I have another question, how do I add this to a date field? As it seems I am having a problem, as date is an Object. Do I simply add the date like this?

table.updateRecordAsync(`${inputConfig.record_id}`, { date: date_added })

Very strange, when you run let current_date = new Date()

console.log returns date in this format.

  1. Wed Sep 29 2021 11:59:31 GMT+0300 (Eastern European Summer Time)

However, system is accepting it in different format (2021-09-29T08:59:15.832Z)

Whilst runinig same instance of new Date() in other complies it returns normal instance of date as - 2021-09-29T08:59:15.832Z

Aha, solutions is following, to apply this function on date for the right formatting .toISOString()

The record ID is already a string, so there’s no need to embed it in another string. This is preferred:

await table.updateRecordAsync(inputConfig.record_id, { date: date_added })

Notice that I also added the “await” keyword. All methods that end in “Async” should use the await keyword at the start of the line because they operate asynchronously.

This is also unnecessary. You can pass the date object directly and Airtable will accept it. The other formatting that you saw in the console is just the default conversion of the object to a string for display purposes. It has no bearing on how Airtable will handle the object itself when passed to the field.