Dates & Time

Dates & Time: JavaScript Date — Calendar + Stopwatch The JavaScript Date object combines a calendar and a clock.

JavaScript Date — Calendar + Stopwatch

The JavaScript Date object combines a calendar and a clock. In test automation we frequently work with dates: validating report timestamps, testing 'registered in last 30 days' filters, checking token expiry. Java's `LocalDateTime` or `Calendar` is what the JS `Date` object is here. But ask yourself: why do date tests break so often — isn't the calendar the same for everyone? It isn't, and the reason is that `Date` is less a calendar than a **timestamp plus a display preference**. The same instant is written as a different date depending on the machine's timezone: the server says 00:30 UTC while your laptop in Istanbul says 03:30 the following day. A "last 30 days" filter that passes locally sits right on the boundary in a CI runner and fails — and only on certain days of the month. Java's `LocalDate`/`ZonedDateTime` split makes that trap visible at the type level; JavaScript's single `Date` hides it, and throws in a bonus surprise: month indexes start at 0 (January = 0). The QA rule: never trust the machine's "now" in a test that generates dates — freeze the clock (fixed clock/mock) or compare in UTC.

Date Comparison — QA Scenario

// ─── Get current date ─────────────────────────────────────── const now = new Date(); console.log("Now:", now.toISOString()); // 2025-06-24T... // ─── Calculate days between two dates ──────────────────────── function daysBetween(d1, d2) { const ms = Math.abs(d2 - d1); // difference in milliseconds return Math.floor(ms / (1000 * 60 * 60 * 24)); // convert to days } const created = new Date('2025-06-01'); const today = new Date(); console.log("Days elapsed:", daysBetween(created, today)); // ─── Token expiry check (invalid after 30 minutes) ─────────── const tokenCreated = new Date(); const expiry = new Date(tokenCreated.getTime() + 30 * 60 * 1000); const isExpired = new Date() > expiry; console.log("Token expired?", isExpired ? "Yes" : "No"); // ─── Warning: getMonth() is 0-indexed! ─────────────────────── const d = new Date('2025-03-15'); console.log("Month (wrong):", d.getMonth()); // 2 (March = index 2!) console.log("Month (correct):", d.getMonth() + 1); // 3 ✅

Micro Lab: JavaScript QA coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: JavaScript QA coding practice

Read the goal and async dependencies

Place await / Promise chain in the right spot

Complete the assertion or expectation line

Run and read console or test output

If flaky, add a wait strategy or retry

What is the safe order for writing and testing JavaScript QA code?

🎬 Why Does new Date(2025, 5, 1) Show June, Not July... or the Reverse?