Back to Blog
timestamp 2025-03-15

Unix Timestamps and Date Handling in Programming

Everything about Unix timestamps, timezone handling, date parsing, and common date-related bugs in software.

Time handling is one of the most deceptively complex areas in programming. Unix timestamps provide a universal way to represent time, but there are many pitfalls to watch out for.

What Is a Unix Timestamp?

A Unix timestamp is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC (the Unix epoch). For example:

  • 0 = January 1, 1970, 00:00:00 UTC
  • 1000000000 = September 9, 2001, 01:46:40 UTC
  • 1700000000 = November 14, 2023, 22:13:20 UTC

Seconds vs Milliseconds

Different systems use different precisions:

// JavaScript uses milliseconds

Date.now() // 1700000000000

// Unix/Linux uses seconds

// $ date +%s // 1700000000

// Python uses seconds (float)

// time.time() // 1700000000.123456

Always check whether a timestamp is in seconds or milliseconds. A common sign: if the number has 10 digits, it is seconds; 13 digits means milliseconds.

The Year 2038 Problem

32-bit systems store Unix timestamps as signed 32-bit integers. The maximum value (2,147,483,647) corresponds to January 19, 2038, 03:14:07 UTC. After this, timestamps will overflow and wrap to negative values (appearing as dates in 1901).

Solution: Use 64-bit timestamps (most modern systems already do).

Timezone Handling

Common Mistakes

1. Assuming server timezone: Always store times in UTC

// Bad

new Date().toString() // "Wed Nov 15 2023 07:13:20 GMT+0900"

// Good - always work with UTC

new Date().toISOString() // "2023-11-14T22:13:20.000Z"

2. DST (Daylight Saving Time) bugs: An hour can repeat or be skipped

3. Timezone name ambiguity: "EST" can mean different things in different countries

Best Practices

  • Store all timestamps in UTC
  • Convert to local time only for display
  • Use ISO 8601 format for string representation (2023-11-14T22:13:20Z)
  • Use IANA timezone identifiers (America/New_York, not EST)

Date Parsing Across Languages

// JavaScript

new Date('2023-11-14T22:13:20Z')

new Date(1700000000 * 1000) // from Unix timestamp

Python

from datetime import datetime, timezone

datetime.fromtimestamp(1700000000, tz=timezone.utc)

datetime.fromisoformat('2023-11-14T22:13:20+00:00')

Common Date Bugs

1. Off-by-one month: JavaScript months are 0-indexed (0=January)

2. Leap year issues: February 29 does not exist every year

3. Timezone offset sign: +09:00 means 9 hours AHEAD of UTC

4. Date overflow: Setting day 31 on a 30-day month silently rolls over

5. Comparing dates: Always compare timestamps, not string representations

Convert Unix timestamps instantly with our Timestamp Converter tool.