# Common Timestamp Errors and How to Fix Them
Timestamp handling is notoriously error-prone. This guide covers the most common timestamp bugs, their causes, and fixes. For reliable timestamp conversion, use our Timestamp Converter.
Error 1: Seconds vs Milliseconds Confusion
The Problem
Your date shows as 1970 instead of the expected date.
The Cause
JavaScript uses milliseconds, but most other systems use seconds. Mixing them up creates dates that are off by a factor of 1000.
// ❌ Treating seconds as milliseconds
const date = new Date(1720425600);
// → Jan 20, 1970
// ✅ Correct: multiply by 1000
const date = new Date(1720425600 * 1000);
// → Jul 8, 2024
The Fix
Always check the digit count:
- 10 digits → seconds → multiply by 1000 for JavaScript
- 13 digits → milliseconds → use directly in JavaScript
function safeTimestamp(ts) {
// Auto-detect seconds vs milliseconds
if (ts < 1e12) return ts * 1000;
return ts;
}
const date = new Date(safeTimestamp(input));
Error 2: Timezone Off-By Hours
The Problem
Your timestamp converts to a time that's several hours off from what you expected.
The Cause
This happens when you create a timestamp in one timezone but interpret it in another.
# ❌ Uses local timezone
import time
ts = int(time.mktime(time.strptime('2024-07-08 10:00:00', '%Y-%m-%d %H:%M:%S')))
# ✅ Explicitly set UTC
from datetime import datetime, timezone
dt = datetime(2024, 7, 8, 10, 0, 0, tzinfo=timezone.utc)
ts = int(dt.timestamp())
The Fix
- Always be explicit about timezones
- Use UTC for storage and computation
- Convert to local time only for display
- Use timezone-aware datetime objects
Error 3: Daylight Saving Time Bugs
The Problem
Your application breaks twice a year when DST transitions occur — events shift by an hour or repeat.
The Cause
During DST transitions, some local times don't exist (spring forward) or occur twice (fall back).
// ❌ Problem: March 10, 2024 2:30 AM doesn't exist in US Eastern
// (clocks jump from 2:00 AM to 3:00 AM)
const date = new Date('2024-03-10T02:30:00-05:00');
// Behavior is implementation-dependent
The Fix
- Use UTC internally: DST doesn't apply to UTC
- Convert to local only for display: At the presentation layer
- Handle ambiguous times: Define behavior for non-existent and duplicate times
- Use timezone libraries: Like
luxon,date-fns-tz, ormoment-timezone
Error 4: Integer Overflow (Y2038)
The Problem
Your application breaks for dates after January 19, 2038.
The Cause
32-bit signed integers max out at 2,147,483,647, which corresponds to January 19, 2038, 03:14:07 UTC.
The Fix
// ❌ 32-bit timestamp
int32_t timestamp = 2147483647; // Overflows after this
// ✅ 64-bit timestamp
int64_t timestamp = 253402300799; // Year 9999
- Use 64-bit integers for timestamps
- Ensure your database column is BIGINT, not INT
- Test with dates beyond 2038
Error 5: Floating-Point Precision
The Problem
Timestamp calculations produce slightly wrong results.
The Cause
Floating-point arithmetic can lose precision with large timestamp values.
// ❌ Floating-point precision loss
const ts = 1720425600.123456;
const result = ts + 0.001; // May not be exactly 1720425600.124456
// ✅ Use integer arithmetic
const tsSeconds = 1720425600;
const tsMillis = 1720425600123;
const tsNanos = 1720425600123456789n; // BigInt for nanoseconds
The Fix
- Use integer arithmetic for timestamp operations
- Use BigInt for nanosecond precision
- Round results explicitly when needed
Error 6: Leap Second Handling
The Problem
Your time synchronization is off by a second during leap second events.
The Cause
Unix timestamps don't account for leap seconds. When a leap second is inserted, the timestamp repeats or skips.
The Fix
- Most systems smear leap seconds over 24 hours (Google's approach)
- For most applications, you can safely ignore leap seconds
- For high-precision systems, use TAI or GPS time instead of UTC
Error 7: Browser Timezone Assumptions
The Problem
Your web application shows different times for different users because new Date() uses the browser's timezone.
The Cause
// ❌ Assumes browser timezone is correct
const date = new Date();
console.log(date.getHours()); // Different per user
// ✅ Work in UTC explicitly
const date = new Date();
console.log(date.getUTCHours()); // Same for all users
The Fix
- Send timestamps from the server rather than relying on client time
- Use
getUTC*methods instead of local methods - Validate client clock: Check if client time is wildly off from server time
- Use libraries like
luxonordate-fnsfor reliable timezone handling
Error 8: Parsing Inconsistent Date Strings
The Problem
Date parsing works in one environment but fails in another.
The Cause
// ❌ Non-standard format — parsing is implementation-dependent
const date = new Date('07/08/2024');
// Could be July 8 or August 7 depending on locale!
// ✅ Use ISO 8601 — universally supported
const date = new Date('2024-07-08T10:00:00Z');
The Fix
- Always use ISO 8601 format for date strings
- Never rely on locale-specific parsing
- Use a parsing library for custom formats
- Validate parsed dates before using them
Error 9: Negative Timestamps
The Problem
Your application crashes or shows errors for dates before 1970.
The Cause
Dates before the Unix epoch (January 1, 1970) have negative timestamps. Some systems don't handle them correctly.
// Date before 1970
const date = new Date('1969-07-20T20:17:00Z'); // Moon landing
const ts = date.getTime(); // -14159020000 (negative!)
// Some systems reject negative timestamps
The Fix
- Test your application with pre-1970 dates
- Use 64-bit integers to handle the full range
- For databases, use TIMESTAMP types that support negative values
- Document whether your system supports pre-1970 dates
Error 10: Race Conditions with Timestamps
The Problem
Two events that happen nearly simultaneously get the same timestamp, making ordering impossible.
The Fix
- Use higher precision (milliseconds or nanoseconds)
- Add a sequence number for tie-breaking
- Use a monotonic clock for measuring elapsed time
- Consider using UUID v7 which includes a timestamp component
Conclusion
Timestamp errors are among the most common bugs in software development. By understanding the pitfalls — seconds vs milliseconds, timezone handling, DST transitions, integer overflow, and parsing inconsistencies — you can write more robust time-handling code. Always use a reliable Timestamp Converter to verify your conversions, and test edge cases like DST transitions and pre-1970 dates.