Guide

Epoch time in programming languages

Every language has its own way of asking "what time is it right now" and turning a timestamp back into a date. Here's the reference for each — including which unit (seconds vs. milliseconds) each one defaults to, which trips people up more than anything else.

Seconds or milliseconds? Check this first.

The single most common bug people hit isn't the syntax — it's the unit. Unix time is defined in seconds, but several languages and platforms (JavaScript, Java, and most JSON-based web APIs) work in milliseconds by default. Mixing the two silently produces a date either 1970 or many thousands of years in the future. Each snippet below notes its default unit explicitly.

Deep dives

Full guide, per language

Each one covers the same three snippets above plus the specific pitfall that trips up that language's developers most.

JavaScript Python PHP Java C# Go Ruby Rust Bash C++ TypeScript Swift Kotlin PowerShell SQL Perl R Dart Scala Excel Google Sheets Lua Elixir Haskell Julia Zig F# COBOL Fortran

Reference

Snippets by language

Current epoch time, plus converting a timestamp to a date, in each language.

Ad placeholder — 728×90 leaderboard

Why do defaults differ across languages?

Most of it comes down to when and where each language grew up. C, Python, PHP, Go, Bash, and most databases inherited the original Unix convention of counting in seconds. JavaScript's Date object was designed in the mid-1990s specifically to match the precision of a browser's clock, so it counts in milliseconds instead — and that choice then spread into JSON APIs and any language interoperating with the browser. Java's System.currentTimeMillis() followed the same millisecond convention for similar reasons, even though its newer java.time package (used in the snippets above) works in seconds.

If you're integrating two systems and dates come out wrong by a factor of exactly 1000 (or land on 1970 or a date thousands of years out), a seconds/milliseconds mismatch is almost always the cause — paste the raw number into the converter above and it will auto-detect which one you're looking at.

Related tools

Doing date math instead of timestamp math? See the full set of date calculators — including a date difference calculator and a business days calculator for scheduling logic. Working with a system that uses a non-Unix epoch (Excel, .NET, GPS time)? The Historical Epoch Converter covers those directly.