Unix Timestamps 解説

理解する Epoch Time

Learn About Timestamps

A Unix timestamp like "1704067200" means January 1, 2024, at midnight UTC. This simple number—seconds since January 1, 1970—is how computers universally represent time. 理解する Unix timestamps is essential for programming, databases, and working with APIs.

Why 1970?

The Unix operating system was developed in the late 1960s at Bell Labs. When designers needed an arbitrary starting point for timekeeping:

  • January 1, 1970 was chosen as a round number near the system's creation
  • It allowed 32-bit integers to cover dates well into the future
  • Starting at midnight UTC kept it simple and universal

This "epoch" became the de facto standard across computing.

Converting Timestamps

Timestamp to Date

TimestampHuman-Readable (UTC)
0January 1, 1970 00:00:00
86400January 2, 1970 00:00:00
1000000000September 9, 2001 01:46:40
1234567890February 13, 2009 23:31:30
1704067200January 1, 2024 00:00:00
2000000000May 18, 2033 03:33:20
2147483647January 19, 2038 03:14:07 (32-bit limit)

Useful Time Intervals

Period
1 minute60
1 hour3,600
1 day86,400
1 week604,800
1 year (approx)31,536,000

Millisecond Timestamps

Many systems use milliseconds instead of seconds:

FormatExample (same moment)Used By
1704067200Unix, PHP, Python
Milliseconds1704067200000JavaScript, Java, JSON APIs
Microseconds1704067200000000Some databases
Nanoseconds1704067200000000000Go, some systems

変換先 convert: Milliseconds ÷ 1000 = 秒

Working with Timestamps in Code

JavaScript

// Current timestamp (milliseconds)
Date.now(); // 1704067200000

// Timestamp to date
new Date(1704067200 * 1000);

// Date to timestamp
Math.floor(new Date('2024-01-01').getTime() / 1000);

Python

import time, datetime

# Current timestamp
time.time()  # 1704067200.0

# Timestamp to datetime
datetime.datetime.fromtimestamp(1704067200)

# Datetime to timestamp
datetime.datetime(2024, 1, 1).timestamp()

SQL

-- MySQL: Current timestamp
SELECT UNIX_TIMESTAMP();

-- Convert timestamp to date
SELECT FROM_UNIXTIME(1704067200);

-- Date to timestamp
SELECT UNIX_TIMESTAMP('2024-01-01');

Advantages of Unix Timestamps

  • Time zone neutral: Always UTC, no DST issues
  • Easy arithmetic: Add/subtract seconds for time math
  • Compact storage: Single integer vs. formatted string
  • Sorting: Natural numeric sort equals chronological sort
  • Universal: Works across languages and systems

When to Use Timestamps vs. Formatted Dates

  • Timestamps: Storage, calculations, APIs, databases
  • Formatted dates: User display, reports, logs

まとめ

Unix timestamps count seconds since January 1, 1970 UTC. This simple system enables universal time representation across computers and programming languages. Remember that some systems use milliseconds (13 digits) instead of seconds (10 digits), and that 32-bit systems face a limit in 2038. For storage and calculations, timestamps are ideal; for display, convert to human-readable formats.

関連記事

Unix Timestamps 解説: Epoch Time for Developers | YounitConverter