Getting the Current Unix Timestamp

Learn how to get the current Unix timestamp in every major programming language. Quick reference with code examples for JS, Python, and more.

Concept

time()

Detailed Explanation

The current Unix timestamp is the number of seconds (or milliseconds) that have passed since January 1, 1970, 00:00:00 UTC. Retrieving this value is one of the most common operations in programming, used for logging, caching, token generation, and performance measurement.

How to get it in major languages:

// JavaScript (milliseconds)
Date.now()              // 1700000000000
Math.floor(Date.now() / 1000)  // seconds
# Python (seconds, as float)
import time
time.time()             # 1700000000.123456
int(time.time())        # seconds, integer
# Bash / Shell
date +%s                # 1700000000
// Java (milliseconds)
System.currentTimeMillis()    // 1700000000000L
Instant.now().getEpochSecond() // seconds
// Go (seconds)
time.Now().Unix()       // 1700000000
time.Now().UnixMilli()  // milliseconds

Seconds vs. milliseconds: Be aware that some languages default to seconds (Python, Go, PHP, Bash) while others default to milliseconds (JavaScript, Java). Mixing them up is a common source of bugs that leads to dates in the far future or displaying as 1970.

Clock sources and accuracy: The system clock that provides timestamps can drift. For distributed systems, consider using NTP (Network Time Protocol) to keep clocks synchronized. When ordering events across multiple servers, relying solely on timestamps can be unreliable; techniques like vector clocks or hybrid logical clocks may be more appropriate.

Performance measurement caveat: For benchmarking code, prefer high-resolution timers like performance.now() in JavaScript or time.perf_counter() in Python rather than epoch timestamps, because they offer sub-millisecond precision and are not affected by system clock adjustments.

Use Case

Getting the current timestamp is essential when generating cache-busting keys, setting token expiration times, or recording the exact moment an event occurred in application logs.

Try It — Timestamp Converter

Open full tool