Help

Re: Script loop based on dates not running when dates are not in same month...

Solved
Jump to Solution
249 0
cancel
Showing results for 
Search instead for 
Did you mean: 
heraklio
4 - Data Explorer
4 - Data Explorer

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);
}

 

 

1 Solution

Accepted Solutions

Hello,

You can use instruction console.log in your script to understand what happens.

For example, i added following instructions to understand better:

Pascal_Gallais_0-1724766818302.png

An the log returns:

Pascal_Gallais_0-1724766901161.png

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:

Pascal_Gallais_1-1724767470513.png

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:

Pascal_Gallais_2-1724767604935.png

Regards,

Pascal

 

See Solution in Thread

2 Replies 2

Hello,

You can use instruction console.log in your script to understand what happens.

For example, i added following instructions to understand better:

Pascal_Gallais_0-1724766818302.png

An the log returns:

Pascal_Gallais_0-1724766901161.png

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:

Pascal_Gallais_1-1724767470513.png

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:

Pascal_Gallais_2-1724767604935.png

Regards,

Pascal

 

heraklio
4 - Data Explorer
4 - Data Explorer

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! 🙏🙏