title: Refactor database query so it is only interested in pairs from today
body: <p>This is from a timeclock app which is used to keep track of employee attendance. The app looks in a database to see if the most recent row has a '1' or a '0' in the column 'eventtype'. If it's a '1' the user is clocked out, if it's a '0' they are still clocked in.</p>
  <p>Basically so long as the shifts don't span overnight, which only happens when someone has forgotten to clock out, then everything works fine.</p>
  <p><strong>But</strong>, because of <code>moment().startOf('day').format</code>, (which is a needed filter, so the code can calculate how many hours they've worked that day), the code crashes if the clock IN was yesterday but the clock OUT was today. Does anyone know how I can work around this?! Have been trying for days without any joy.</p>
  <p>Thank you!</p>
  <pre><code>db.query('SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME &gt;= \'' + moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC', function(err, today_rows) {
              today_rows.push({
                  DATETIME: moment().format('YYYY-MM-DD HH:mm:ss')
              });
              splitRows = today_rows.reduce(function(result, value, index, array) {
                  if (index % 2 === 0)
                      result.push(array.slice(index, index + 2));
                  return result;
              }, []);
              splitRows.forEach(pair =&gt; {
                  totalMinutes += Math.round(moment.duration(moment(pair[1].DATETIME).diff(moment(pair[0].DATETIME))).asMinutes());
              });
          }
  </code></pre>
  <p>This WOULD cause an issue</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>EVENTID</th>
  <th>ID</th>
  <th>DATETIME</th>
  <th>EVENTTYPE</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>172</td>
  <td>4</td>
  <td>13/09/2021 08:30</td>
  <td>0</td>
  </tr>
  <tr>
  <td>175</td>
  <td>4</td>
  <td>13/09/2021 14:00</td>
  <td>1</td>
  </tr>
  <tr>
  <td>176</td>
  <td>4</td>
  <td>13/09/2021 14:36</td>
  <td>0</td>
  </tr>
  <tr>
  <td>180</td>
  <td>4</td>
  <td>13/09/2021 17:30</td>
  <td>1</td>
  </tr>
  <tr>
  <td>203</td>
  <td>4</td>
  <td>14/09/2021 11:52</td>
  <td>0</td>
  </tr>
  <tr>
  <td>204</td>
  <td>4</td>
  <td>16/09/2021 20:02</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>This would not, because the final session is wrapped up within the space of one day</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>EVENTID</th>
  <th>ID</th>
  <th>DATETIME</th>
  <th>EVENTTYPE</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>172</td>
  <td>4</td>
  <td>13/09/2021 08:30</td>
  <td>0</td>
  </tr>
  <tr>
  <td>175</td>
  <td>4</td>
  <td>13/09/2021 14:00</td>
  <td>1</td>
  </tr>
  <tr>
  <td>176</td>
  <td>4</td>
  <td>13/09/2021 14:36</td>
  <td>0</td>
  </tr>
  <tr>
  <td>180</td>
  <td>4</td>
  <td>13/09/2021 17:30</td>
  <td>1</td>
  </tr>
  <tr>
  <td>203</td>
  <td>4</td>
  <td>14/09/2021 11:52</td>
  <td>0</td>
  </tr>
  <tr>
  <td>204</td>
  <td>4</td>
  <td>14/09/2021 20:02</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>