πŸ”§ 10 Date-and-Time Bugs I Still See in Production (and How to Avoid Them)

β€’3 min read

A senior developer's checklist of the mistakes that break schedules, invoices, and reports. Includes code samples and free tools to fix every issue.

After more than a decade writing backend code, I've fixed the same time-related bugs over and over.
Below are ten of the worst offenders, why they happen, and a proven way to avoid each one. I've linked free tools and API calls from Pastetime so you can try the fixes right away.

🐞 Bug 1 – Storing Local Time in the Database

What you see
Reports jump an hour forward or back every spring and autumn.

Why it happens
Local timestamps are ambiguous when daylight‐saving rules shift or the server moves to a new zone.

Fix
Store UTC only, convert on display.

Quick test
Paste any local time into the UTC-to converter to see the safe format.

🐞 Bug 2 – Mixing Unix Seconds and Milliseconds

Symptom
Graphs plot dates in the year 51345 or collapse to 1970.

// Safe JS conversion
const unixMs = 1754136000000; // milliseconds
const date = new Date(unixMs); // correct
const unixS = Math.floor(unixMs / 1000);

Need a spot-check? POST /api/convert-timestamp in the API docs returns both seconds and milliseconds.

🐞 Bug 3 – Hard-coding Weekends

Problem Business-day counts are wrong where the weekend is Friday–Saturday.

curl -X POST "https://www.pastetime.com/api/business-days" \
  -H "Content-Type: application/json" \
  -d '{
        "startDate": "2025-07-01",
        "endDate":   "2025-07-10",
        "weekendDays": ["Friday","Saturday"]
      }'

Or use the UI at /business-days-calculator.

🐞 Bug 4 – Ignoring DST in Cron Jobs

Jobs drift by an hour when daylight-saving hits. Fix: run cron in UTC or use a scheduler that's zone-aware.

🐞 Bug 5 – DIY Natural-Language Parsing

Regex can't parse "next Friday 3 pm Berlin." POST /api/convert handles it and returns ISO, Unix, and human text.

🐞 Bug 6 – Silent JavaScript Date Coercion

new Date('2025-07-02') can flip to the previous day in some browsers. Always include the timezone offset or use a library like date-fns-tz.

🐞 Bug 7 – Hard-coded Holiday Lists

Holiday arrays go stale. Pass holidays at runtime via the Business Days API, or store them centrally.

🐞 Bug 8 – Leap Year (and Leap Second) Edge Cases

Date math fails on Feb 29 or during leap seconds. Trust a tested library or API instead of hand-rolled arithmetic.

🐞 Bug 9 – Server Timezone Drift

Containers boot in UTC, code expects local. Pin the TZ env var or convert to UTC everywhere.

🐞 Bug 10 – Sorting Raw Date Strings

2025-07-2 sorts before 2025-07-12. Use padded ISO (YYYY-MM-DD) or Unix timestamps, not loose strings.

πŸ“‹ Cheat Sheet

Bug Fast Fix Tool / API
Local time in DB Store UTC only /utc-to
Seconds vs ms Standardise or convert /api/convert-timestamp
Hard-coded weekends Pass config /api/business-days
DST cron drift Run in UTC Upcoming cron guide
DIY NLP parsing Use API /api/convert
JS coercion Include TZ offset Timestamp guide

πŸš€ Wrap-Up

Date bugs rarely crash apps, yet they quietly break schedules and revenue. Use the Pastetime web tools for quick fixes or call the API when you need reliable automation.

Have a horror story or feature request? Open an issue or ping me on X.

Support the project on Ko-fi or Buy Me a Coffee.