Skip to content
← All Tools
๐Ÿ”’All processing in your browser ๐ŸšซNo uploads stored ๐Ÿ›ก๏ธPrivacy-first conversion tools โœ“No login required
Tutorial

Common Timestamp Bugs and How to Avoid Them

Bill Crawford — Developer Guide — 2026  ยท  Last updated January 18, 2026

Timestamp bugs are uniquely frustrating โ€” they're often intermittent (only at certain times of year or in certain timezones), hard to reproduce locally, and can cause real data loss. Here are the most common ones with fixes.

Connect on LinkedIn โ†’

Convert timestamps instantly: Diagnose timezone and precision issues by converting between Unix timestamps and readable dates.

Open Timestamp Converter โ†’

Table of Contents

  1. Bug 1: Seconds vs Milliseconds Confusion
  2. Bug 2: Storing Local Time Instead of UTC
  3. Bug 3: Daylight Saving Time Edge Cases
  4. Bug 4: Database Timezone Mismatches
  5. Bug 5: Off-By-One Day Errors
  6. Bug 6: Comparing Timestamps of Different Precision
  7. Bug 7: Cache Keys With Date.now()
  8. Quick Reference: Safe Timestamp Practices
  9. Why Timestamps Are Surprisingly Hard
  10. Timestamps in Databases: Storage vs Display
  11. Testing Timestamp Code

Bug 1: Seconds vs Milliseconds Confusion

Mixing seconds-based timestamps (Unix, Python, most databases) with milliseconds-based timestamps (JavaScript Date.now(), Java, MongoDB) causes dates that display as 1970 or 55,000 years in the future.

new Date(1740355200)        // โœ— Jan 20 1970
new Date(1740355200 * 1000) // โœ“ Feb 24 2026

// Detect automatically:
const isMs = timestamp > 1e12; // 13 digits = milliseconds

Bug 2: Storing Local Time Instead of UTC

new Date().toString() and datetime.now() save the server's local time, which changes when the server is moved or DST kicks in. Always use UTC: new Date().toISOString(), datetime.now(timezone.utc), UTC_TIMESTAMP() in MySQL, NOW() AT TIME ZONE 'UTC' in PostgreSQL.

Bug 3: Daylight Saving Time Edge Cases

"Spring forward" makes one hour (2:00โ€“3:00 AM) not exist โ€” scheduled jobs targeting 2:30 AM don't fire. "Fall back" makes one hour happen twice โ€” jobs may fire twice or log entries appear to go backwards. Fix: schedule in UTC, which has no DST.

Bug 4: Database Timezone Mismatches

MySQL's DATETIME type stores no timezone โ€” it stores whatever you give it. If your app server is UTC but the database connection is configured to a different timezone, timestamps are silently shifted on read and write. Always set SET time_zone = '+00:00' in MySQL connections. Use PostgreSQL's TIMESTAMP WITH TIME ZONE which stores UTC correctly.

Bug 5: Off-By-One Day Errors

2026-02-24 22:00:00 PST is 2026-02-25 06:00:00 UTC. Reports grouped "by date" show different results depending on which timezone is used. Decide which timezone your business logic uses for "a day" and apply it consistently everywhere.

Bug 6: Comparing Timestamps of Different Precision

Comparing a seconds-precision database timestamp with a milliseconds-precision API value causes incorrect inequality comparisons. Normalise to the same unit before comparing.

Bug 7: Cache Keys With Date.now()

Date.now() changes every millisecond โ€” using it directly in a cache key defeats the cache. Round to your desired window: Math.floor(Date.now() / 300000) * 300000 for 5-minute buckets.

Quick Reference: Safe Timestamp Practices

Why Timestamps Are Surprisingly Hard

Timestamps seem simple โ€” they're just numbers representing points in time. But the complexity hides in the layers of abstraction between "what time is it" and "what number do I store." A Unix timestamp is seconds since January 1, 1970 UTC, but the same moment in time has a different representation in every time zone, and the offset between UTC and a local zone changes twice a year in most places due to daylight saving time.

This means that "March 10, 2024 at 2:30 AM" in the US Eastern time zone doesn't exist โ€” clocks jumped from 2:00 AM directly to 3:00 AM. If your application allows a user to select that date and time, what do you store? The answer depends on your stack, your database, and whether you've thought about this edge case at all. Most applications haven't.

Timestamps in Databases: Storage vs Display

The single biggest source of timestamp bugs in production systems is confusion between storage format and display format. PostgreSQL's TIMESTAMP WITH TIME ZONE stores everything internally as UTC and converts on display. MySQL's TIMESTAMP type also converts to UTC for storage, but DATETIME stores the literal value without conversion. This means the same application code can produce different results depending on which column type was chosen, and the bug only appears when the application server and database server are in different time zones โ€” which often means it works in development and breaks in production.

SQL Server's DATETIMEOFFSET stores both the UTC time and the original offset, which is the most information-preserving approach. If you need to know that a meeting was scheduled at "3 PM Tokyo time" rather than just the UTC equivalent, you need the offset. But this comes with its own complexity: two DATETIMEOFFSET values can represent the same instant but have different offsets, so equality comparisons must normalize to UTC first.

Testing Timestamp Code

Timestamp bugs are notoriously hard to test because they depend on wall-clock time, server configuration, and calendar rules. The most reliable approach is to make your clock injectable โ€” wrap system time access behind an interface so tests can substitute a fixed or controlled clock. This lets you test DST transitions, year boundaries, leap seconds, and far-future dates without waiting for them to occur.

Pay special attention to testing around midnight UTC, around DST transitions in your target time zones, around the epoch (timestamp 0), and with timestamps that are milliseconds when your code expects seconds. The Date.now() function in JavaScript returns milliseconds, while most APIs and databases expect seconds โ€” dividing by 1000 and accidentally truncating to an integer is one of the most common timestamp bugs in web applications.

Further reading: MDN โ€” JavaScript Date

BC
Bill Crawford
Founder, Data Conversion Center

Bill Crawford is a data systems developer and technical founder with over 30 years of professional experience in accounting, finance, and business operations.

He holds a Bachelor's degree in Accounting and has spent more than three decades working within financial and operational environments. Over the past 10 years, he has been heavily involved in the development, implementation, and refinement of financial and enterprise data systems for both Fortune 500 companies and smaller organizations.

His work bridges finance and technology — combining deep domain knowledge in structured reporting and accounting workflows with hands-on SQL development and database architecture experience.

Bill founded DataConversionCenter.com to build practical, browser-based tools that simplify complex data challenges, including:

Rather than focusing on theoretical examples, his tools and articles are informed by real-world challenges encountered in enterprise reporting systems, financial databases, and operational data environments.

Professional Background

Bill's mission is to reduce friction in data workflows — particularly for professionals working with structured financial, operational, and reporting data.