Start learning free

LESSON 165 OF 173 · BACKEND: BACKEND WITH AI

Dates the AI Got Wrong in JavaScript

Practice

An AI assistant wrote GET /orders?since=… for the shop: the orders placed on or after a date. Customers type the date day first, like 18/04/2026. The database keeps each order's date year first, like 2026-04-18. The AI compared the two texts as they are:

app.get("/orders", (req, res) => {
  const since = req.query.since;
  const rows = db.prepare("SELECT * FROM orders").all();
  const result = [];
  for (const row of rows) {
    if (row.ordered >= since) {
      result.push(row);
    }
  }
  res.json(result);
});

JavaScript compares strings character by character, and the first difference decides. "2026-03-02" >= "18/04/2026" is true, because "2" comes after "1". So since=18/04/2026 answered all 7 orders and since=30/04/2026 answered none. The impossible 31/04/2026 got 200 and an empty list, and so did a request without since.

The fix: turn each text into a real date with new Date(text), then compare dates. The one format new Date reads the same way everywhere is the ISO format: year first, with dashes. It never complains: a text it can't read gives an Invalid Date, and a day that doesn't exist quietly rolls over into the next month. Turning the date back into text shows it:

const d = new Date("2026-05-07");
console.log(d.toISOString().slice(0, 10));     // 2026-05-07
const bad = new Date("2026-02-30");            // there is no 30 February
console.log(bad.toISOString().slice(0, 10));   // 2026-03-02
console.log(isNaN(new Date("soon")));          // true: an Invalid Date
console.log("07/05/2026".split("/"));          // [ '07', '05', '2026' ]

The requests and the answers your route must give:

GET /orders?since=18/04/2026  →  200  [{"customer_id": 3, "id": 4, "ordered": "2026-04-18", "status": "shipped"}, …]   (orders 4 to 7)
GET /orders?since=30/04/2026  →  200  [{"customer_id": 4, "id": 5, "ordered": "2026-05-07", "status": "paid"}, …]   (orders 5 to 7)
GET /orders?since=01/06/2026  →  200  []
GET /orders?since=31/04/2026  →  400  {"error": "since must be a date like 18/04/2026"}
GET /orders  →  400  {"error": "since must be a date like 18/04/2026"}

Your task: rewrite the function of the /orders route. Split since at "/", join the year, the month and the day with dashes, and read that text with new Date. Answer 400 with the error when it isn't a real date. Read each order's ordered with new Date as it is, and answer the orders placed on or after since.


Assignment
Follow the Coding Tutorial to rewrite the /orders route so it compares real dates instead of texts.


Hint
new Date never throws, so check the date yourself: isNaN(since) is true for an Invalid Date, and since.toISOString().slice(0, 10) differs from the text you built when the day doesn't exist. Read since as (req.query.since || ""), so a missing date lands in the same check. Use >= so an order placed on the since day itself is kept.


STEP 1 OF 12
    Ctrl + Enter to check

    Ai Timezone in JavaScript

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free