Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Sep 28, 2021 05:45 AM
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')
Solved! Go to Solution.
Sep 28, 2021 06:15 AM
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.
Sep 28, 2021 06:15 AM
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.
Sep 29, 2021 01:49 AM
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 })
Sep 29, 2021 02:01 AM
Very strange, when you run let current_date = new Date()
console.log returns date in this format.
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
Sep 29, 2021 02:05 AM
Aha, solutions is following, to apply this function on date for the right formatting .toISOString()
Sep 29, 2021 06:51 AM
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.