How can I filter Airtable records by a specific date without getting the first entry?

I’m stuck with an Airtable filtering issue. I want to get a record with a specific date (8/01/2022) in the Name field. But my code keeps returning the first entry in the table instead.

Here’s what I’m doing:

const isoDate = new Date('08/01/2022').toISOString().slice(0, 10);

const myBase = new Airtable({ apiKey: myKey }).base(myBaseId);

myBase('myTable').select({
  filterByFormula: `DATESTR({Name})='${isoDate}'`,
  view: 'Grid view',
}).eachPage(
  function handlePage(rows, getNextPage) {
    rows.forEach(row => {
      const newItem = {
        date: row.get('Name'),
        game: row.get('games'),
      };
      updateData(newItem);
    });
    try {
      getNextPage();
    } catch {
      return;
    }
  },
  function handleError(error) {
    if (error) {
      console.error(error);
      return;
    }
  }
);

The result I get is always the first record in my table (date: ‘2022-07-29’). What am I doing wrong? How can I make it return the correct date?

I’ve dealt with similar Airtable filtering challenges before. One thing that often trips people up is the date format mismatch between JavaScript and Airtable. While your approach is close, you might want to try adjusting your date formatting.

Instead of using .toISOString().slice(0, 10), which gives you ‘YYYY-MM-DD’, try formatting the date to match Airtable’s expected format:

const formattedDate = new Date(‘08/01/2022’).toLocaleDateString(‘en-US’, { year: ‘numeric’, month: ‘2-digit’, day: ‘2-digit’ });

Then use this formattedDate in your filterByFormula:

filterByFormula: DATESTR({Name})='${formattedDate}'

This should align your date format with what Airtable expects. If you’re still having issues, double-check that your ‘Name’ field is indeed a Date field in Airtable and not a string. Let me know if this helps!

hey there! looks like ur date format might be the issue. try using ‘YYYY-MM-DD’ instead of ‘MM/DD/YYYY’ in ur code. so change it to ‘2022-08-01’. also double-check if the ‘Name’ field actually contains dates. hope this helps!

I encountered a similar issue with Airtable date filtering. The problem likely stems from how Airtable internally handles dates. To resolve this, try modifying your filterByFormula to use the IS_SAME function instead. Here’s an adjusted version:

filterByFormula: `IS_SAME({Name}, '${isoDate}', 'day')`

This approach compares the dates at the day level, which should accurately match your target date. Additionally, ensure your ‘Name’ field is set as a Date field type in Airtable. If it’s a text field, you might need to convert it first using the DATETIME_PARSE function within your formula. Let me know if this resolves your issue.