Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Aug 27, 2024 06:02 AM
Hi community,
I'm struggling to understand why this whatsoever perfectly working tiny piece of script stops doing so when (seemingly) the 'start' date and the 'end' date are not in the same month.
I am rather new to Airtable script editor so I couldn't find a step-by-step run mode, or a variable value watcher, but it seems that the 'days' variable is not computed correctly when 'start' and 'end' belong to a different month (for instance, 2024-07-15 and 2024-08-07 - however works perfectly with 2024-07-15 and 2024-07-31, and then 2024-08-01 and 2024-08-07)... leading to not entering the 'for' loop at all.
Any clue anyone? Many thanks in advance!
let table = base.getTable('Man.days');
let inputConfig = input.config();
const start = new Date(inputConfig.StartDate);
const end = new Date(inputConfig.EndDate);
const days = end.getDate() - start.getDate();
var date = start;
for (let i = 0; i <= days; i++) {
let record = await table.createRecordAsync({
['Installation ID']: [{id: inputConfig.InstallationID}],
'Date': date,
['Technician']: [{id: inputConfig.Technician}],
});
date.setDate(date.getDate() + 1);
}
Solved! Go to Solution.
Aug 27, 2024 07:07 AM
Hello,
You can use instruction console.log in your script to understand what happens.
For example, i added following instructions to understand better:
An the log returns:
Meaning that getDate() function returns only the day part of your dates, explaining the issue when start and end are not on the same month.
You could try computing the number of days like this:
Not that if SartDate and EndDate are some fields in your table, you could add a formula field using the DATETIME_DIFF FUNCTIONS to get this result directly in your table:
Regards,
Pascal
Aug 27, 2024 07:07 AM
Hello,
You can use instruction console.log in your script to understand what happens.
For example, i added following instructions to understand better:
An the log returns:
Meaning that getDate() function returns only the day part of your dates, explaining the issue when start and end are not on the same month.
You could try computing the number of days like this:
Not that if SartDate and EndDate are some fields in your table, you could add a formula field using the DATETIME_DIFF FUNCTIONS to get this result directly in your table:
Regards,
Pascal
Aug 27, 2024 07:17 AM
Dear @Pascal_Gallais-
Thank you so much for your help - indeed the getDate() function was to blame here, I implemented your code suggestion and it now works perfectly well.
Thanks as well for the console.log hint - should definitely come in handy in the future.
Merci! 🙏🙏