How Unix Timestamps Work
What is Unix Time?
Unix time (also called epoch time, POSIX time, or Unix timestamp) is a system for describing points in time. It represents the number of seconds that have elapsed since the "Unix epoch" - January 1, 1970, 00:00:00 UTC - not counting leap seconds.
Key Facts:
- • Epoch Start: January 1, 1970, 00:00:00 UTC
- • Unit: Seconds (though milliseconds and microseconds are also used)
- • Timezone: Always based on UTC (Coordinated Universal Time)
- • Range: Can represent dates from 1970 to 2038 (32-bit) or beyond (64-bit)
Timestamp Formats
1. Seconds (10 digits)
The standard Unix timestamp format representing seconds since epoch.
Example: 1735344072
Represents: January 27, 2025, 16:41:12 UTC
2. Milliseconds (13 digits)
Used in JavaScript and many web applications for higher precision.
Example: 1735344072000
Same time as above but with millisecond precision
3. Microseconds (16 digits)
Used in high-precision systems and some databases.
Example: 1735344072000000
Same time with microsecond precision
Conversion Process
Epoch to Date
Converting a Unix timestamp to a human-readable date involves these steps:
- Identify the timestamp format (seconds, milliseconds, or microseconds)
- Convert to milliseconds if necessary (JavaScript Date objects use milliseconds)
- Create a Date object from the timestamp
- Format the date according to the desired timezone and format
Example Conversion:
- Input: 1735344072 (seconds)
- Convert to milliseconds: 1735344072 × 1000 = 1735344072000
- Create Date: new Date(1735344072000)
- Format: "2025-01-27 16:41:12 UTC"
Date to Epoch
Converting a date to Unix timestamp:
- Parse the input date string or date components
- Create a Date object (automatically handles timezone conversion to UTC)
- Get the timestamp in milliseconds using getTime()
- Convert to seconds by dividing by 1000 (if seconds format is desired)
Timezone Handling
Unix timestamps are always in UTC, but when displaying dates to users, we often need to convert to local timezones:
- UTC: The standard reference timezone (GMT+0)
- Local Time: Adjusted for the user's timezone
- Specific Timezones: Convert to any timezone (EST, PST, JST, etc.)
Common Use Cases
Development
- • API timestamps
- • Database records
- • Log file analysis
- • Caching expiration
System Administration
- • Server logs
- • Backup timestamps
- • Monitoring data
- • Scheduled tasks
Limitations and Considerations
Year 2038 Problem
32-bit systems will face an overflow issue on January 19, 2038, at 03:14:07 UTC. Modern 64-bit systems extend this limit far into the future.
Leap Seconds
Unix time doesn't account for leap seconds, which are occasionally added to UTC. This means Unix time can be off by several seconds from actual UTC.