100% Free Forever — No signup, no paywalls, no limits.
DevKit
Utilities 4 min read December 13, 2025

Unix Timestamps: Everything You Need to Know

Understanding Unix timestamps, epoch time, timezone handling, and common conversion scenarios for developers.

DK

DevKit Team

Engineering

Share:

Unix timestamps are the universal language of time in computing. They're simple, timezone-agnostic, and work across every programming language. But despite their simplicity, timestamps trip up developers in surprisingly common ways.

What Is a Unix Timestamp?

A Unix timestamp is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC — known as the Unix epoch. It's also called "epoch time" or "POSIX time."

javascript
// Current Unix timestamp (seconds)
const timestamp = Math.floor(Date.now() / 1000);
console.log(timestamp); // e.g., 1722883200

// Current timestamp (milliseconds) — JavaScript default
const msTimestamp = Date.now();
console.log(msTimestamp); // e.g., 1722883200000

Warning

JavaScript's Date.now() returns milliseconds, not seconds! Most Unix systems and APIs expect seconds. Always divide by 1000 when sending to a backend.

Converting Timestamps to Dates

javascript
// Timestamp to Date object
const ts = 1722883200;
const date = new Date(ts * 1000); // multiply by 1000 for JS
console.log(date.toISOString()); // "2024-08-05T16:00:00.000Z"
console.log(date.toLocaleString()); // Local time representation

// Date to timestamp
const newTs = Math.floor(new Date('2024-08-05').getTime() / 1000);
console.log(newTs); // 1722816000

Timezone Handling

Unix timestamps are always in UTC. This is their greatest strength — they're immune to timezone confusion. Convert to a local timezone only when displaying to the user. Store and transmit timestamps in UTC.

  • Store timestamps in UTC (Unix epoch or ISO 8601).
  • Convert to local time only at the presentation layer.
  • Use toISOString() for storage and API calls.
  • Use toLocaleString() for user-facing display.
  • Never use getHours() for server-side logic — it uses local time.

Tip

Use ISO 8601 strings (e.g., "2024-08-05T16:00:00Z") for API communication. They're human-readable, timezone-explicit, and sortable as strings.

Common Timestamp Operations

javascript
// Add time to a timestamp
const now = Math.floor(Date.now() / 1000);
const oneHourLater = now + 3600;        // +1 hour
const oneDayLater = now + 86400;        // +1 day
const oneWeekLater = now + 604800;      // +1 week

// Difference between two timestamps (in seconds)
const diff = endTs - startTs;
const hours = Math.floor(diff / 3600);
const minutes = Math.floor((diff % 3600) / 60);
const seconds = diff % 60;

// Human-readable relative time
function timeAgo(ts) {
  const seconds = Math.floor(Date.now() / 1000) - ts;
  if (seconds < 60) return 'just now';
  if (seconds < 3600) return Math.floor(seconds / 60) + 'm ago';
  if (seconds < 86400) return Math.floor(seconds / 3600) + 'h ago';
  return Math.floor(seconds / 86400) + 'd ago';
}

The Year 2038 Problem

On January 19, 2038, 32-bit Unix timestamps will overflow. Systems using 32-bit integers for timestamps will wrap around to a negative number, causing widespread bugs. Most modern systems use 64-bit integers and are unaffected, but legacy systems may need updates.

Warning

If you're maintaining legacy C or embedded systems, audit your timestamp handling before 2038. Use 64-bit time_t or switch to a custom epoch with smaller values.

Timestamp Formats Cheat Sheet

  • Unix seconds: 1722883200 (used by most APIs)
  • Unix milliseconds: 1722883200000 (JavaScript default)
  • ISO 8601: 2024-08-05T16:00:00Z (human-readable, timezone-explicit)
  • RFC 2822: Mon, 05 Aug 2024 16:00:00 GMT (email format)
  • Relative: -1d, +2h (useful for query parameters)

"Always store time in UTC. Convert to local time only when displaying to users. This single rule eliminates 90% of timezone bugs."

Unix timestamps are deceptively simple. Master them and you'll avoid the timezone bugs that plague so many applications. When in doubt, use UTC, use ISO 8601, and test with edge cases like midnight and daylight saving transitions.

Why This Matters in 2026

Utility tools — timestamp converters, UUID generators, cron expression builders, number base converters — are the unsung heroes of development. They handle the small but critical tasks that every developer encounters daily. In 2026, these tools have evolved with new standards like UUID v7, the TC39 Temporal API proposal, and modern cron scheduling platforms. Understanding these utilities deeply saves time and prevents subtle bugs that can take hours to trace.

Key Takeaways

  • Use UUID v7 instead of v4 for database primary keys — it is timestamp-sortable
  • Store timestamps in UTC, convert to local time only at the presentation layer
  • Use crypto.randomUUID() for hardware-accelerated UUID generation in browsers
  • Always confirm the timezone when working with cron expressions
  • JavaScript Date.now() returns milliseconds, not seconds — divide by 1000 for APIs
  • Use ISO 8601 strings for API communication — they are sortable and timezone-explicit

Common Mistakes to Avoid

  • Using UUID v4 for database primary keys, causing B-tree index fragmentation
  • Confusing seconds and milliseconds when converting timestamps across languages
  • Not specifying the timezone in cron jobs, causing jobs to run at wrong times
  • Using getHours() for server-side logic — it uses local time, not UTC
  • Generating UUIDs with Math.random() instead of crypto.randomUUID()
  • Storing timestamps in local time instead of UTC, causing timezone bugs

Warning

The Year 2038 problem is real for 32-bit systems. If you maintain legacy C/C++ code, embedded firmware, or older database schemas, audit your timestamp handling now. Use 64-bit time_t or switch to a custom epoch with smaller values.

Best Practices

  • Default to UUID v7 for new systems — sortable, index-friendly, RFC standard
  • Store all timestamps in UTC (Unix epoch or ISO 8601 with Z suffix)
  • Use toISOString() for storage and API calls, toLocaleString() for display
  • Specify timezone explicitly in cron job definitions wherever supported
  • Use crypto.randomUUID() in browsers, uuid v4 library in Node.js if needed
  • Test timezone edge cases: midnight UTC, DST transitions, 30-minute offset zones

Tip

When converting timestamps between languages, always check the unit first. JavaScript uses milliseconds, Python uses seconds, Go uses both, and PostgreSQL stores microseconds. A simple unit mismatch causes bugs that are incredibly hard to trace.

Quick Reference

Here are quick reference snippets for the most common utility operations across JavaScript and other languages:

javascript
// UUID v7 (sortable, index-friendly)
const uuid = crypto.randomUUID(); // v4 in browsers
// For v7, use the uuid library: import { v7 } from "uuid";

// Timestamp conversions
const now = Date.now(); // milliseconds
const unixSeconds = Math.floor(Date.now() / 1000);
const isoString = new Date().toISOString(); // "2026-08-06T00:00:00.000Z"

// Number base conversion
const hex = (255).toString(16); // "ff"
const binary = (10).toString(2); // "1010"
const decimal = parseInt("ff", 16); // 255

// Cron expression: every 15 minutes
const cron = "*/15 * * * *";

Real-World Example

A practical scenario: generating sortable IDs for a database insert and converting timestamps for a multi-timezone scheduling system. This pattern works across JavaScript backends and browser clients:

javascript
// Generate sortable UUIDs for bulk database insert
function generateIds(count) {
  return Array.from({ length: count }, () => crypto.randomUUID());
}

// Convert UTC timestamp to user's local time
function formatForUser(utcTimestamp, timezone) {
  return new Date(utcTimestamp * 1000).toLocaleString("en-US", {
    timeZone: timezone,
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  });
}

// Usage
const ids = generateIds(1000); // Bulk generate
const localTime = formatForUser(1722883200, "America/New_York");

Tools and Resources

"The best utility is the one you do not have to think about. It just works, every time, without surprises."

Utility tools handle the small but critical tasks that every developer faces daily. Use UUID v7 for sortable database keys, store timestamps in UTC, specify timezones in cron jobs, and always check units when converting between languages. These small choices prevent the subtle bugs that waste hours of debugging time.

Advertisement
32 tools ready to use

Ready to boost your workflow?

No accounts. No uploads. No limits. Just open a tool and start working.

Browse All Tools
Free forever
No signup
100% private