Timestamp

Unix Timestamp Conversion: Complete Guide

Everything about Unix timestamps: what they are, how to convert them, timezone handling, and best practices.

Introduction

Timestamps are numeric representations of dates and times, essential for logging, scheduling, and data synchronization. Understanding timestamp formats and conversions is crucial for backend development and API integration.

This guide covers Unix timestamps, ISO 8601, and common conversion scenarios.

Unix Timestamp

Unix time counts seconds since January 1, 1970 00:00:00 UTC (the Unix epoch):

// Unix timestamp (seconds)
  1704067200  // January 1, 2024 00:00:00 UTC

  // Milliseconds (JavaScript)
  1704067200000

  // Current timestamp
  const now = Math.floor(Date.now() / 1000);
  // Result: 1707321600 (example)
  • Format: Integer (seconds or milliseconds)
  • Timezone: Always UTC
  • Range: Signed 32-bit: 1901-2038, 64-bit: billions of years

ISO 8601 Format

ISO 8601 is a human-readable international standard:

// Basic format
  2024-01-01T00:00:00Z

  // With timezone offset
  2024-01-01T08:00:00+08:00

  // Milliseconds
  2024-01-01T00:00:00.123Z

  // Date only
  2024-01-01
  • T: Separates date and time
  • Z: Indicates UTC ("Zulu" time)
  • +/-HH:MM: Timezone offset from UTC

Common Conversions

Unix to ISO 8601 (JavaScript):

// From seconds
  const timestamp = 1704067200;
  const date = new Date(timestamp * 1000);
  const iso = date.toISOString();
  // Result: "2024-01-01T00:00:00.000Z"

  // From milliseconds
  const ms = 1704067200000;
  const iso = new Date(ms).toISOString();
  // Result: "2024-01-01T00:00:00.000Z"

ISO to Unix (JavaScript):

const iso = "2024-01-01T00:00:00.000Z";
  const timestamp = Math.floor(new Date(iso).getTime() / 1000);
  // Result: 1704067200

Python:

import datetime

  # Unix to ISO
  timestamp = 1704067200
  dt = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
  iso = dt.isoformat()
  # Result: '2024-01-01T00:00:00+00:00'

  # ISO to Unix
  iso = "2024-01-01T00:00:00Z"
  dt = datetime.datetime.fromisoformat(iso.replace('Z', '+00:00'))
  timestamp = int(dt.timestamp())
  # Result: 1704067200

Timezone Handling

Always be explicit about timezones:

// UTC (Recommended for storage)
  2024-01-01T00:00:00Z

  // US Eastern Time (UTC-5)
  2024-01-01T00:00:00-05:00

  // Tokyo (UTC+9)
  2024-01-01T00:00:00+09:00

  // Convert to user's timezone on display
  const utc = "2024-01-01T00:00:00Z";
  const local = new Date(utc).toLocaleString();
  // Result: "1/1/2024, 8:00:00 AM" (if user is UTC+8)

Common Pitfalls

  • Seconds vs Milliseconds: JavaScript uses ms, most systems use seconds
  • Timezone confusion: Always store in UTC, convert for display
  • Year 2038 problem: 32-bit timestamps overflow on Jan 19, 2038
  • Leap seconds: Not handled by Unix time
  • Local time: Avoid storing local time without timezone info

Best Practices

  • Store timestamps in UTC
  • Use 64-bit integers for Unix timestamps
  • Use ISO 8601 for human-readable formats
  • Always include timezone in ISO format
  • Convert to user's timezone only for display
  • Use database timestamp types (not strings)
  • Test with different timezones and DST transitions

Database Examples

-- PostgreSQL
  CREATE TABLE events (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE
  );

  -- Query with timezone
  SELECT
    created_at AT TIME ZONE 'UTC' as utc_time,
    created_at AT TIME ZONE 'America/New_York' as ny_time
  FROM events;

  -- MySQL
  CREATE TABLE logs (
    id INT PRIMARY KEY,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    datetime DATETIME
  );

Try Our Tools

Explore our free online developer tools:

Related Articles

Base64

What is Base64 Encoding and How Does it Work?

Learn everything about Base64 encoding: what it is, how it works, when to use it, and practical examples for developers.

Base64

Base64 vs Binary: Understanding the Difference

Deep dive into the differences between Base64 and Binary encoding. Learn which format to use for your specific use case.

Base64

How to Embed Images in HTML Using Base64

Complete guide to embedding images directly in HTML using Base64 data URIs. Includes performance tips and best practices.