Unix Timestamp Code Examples

Getting the current epoch, converting a timestamp to a date, and converting a date back — in 21 languages, with the pitfalls each one has.

Why every language does this differently

The underlying value is identical everywhere — an integer counting seconds since 1970 — but each language picked a different default resolution and a different attitude to time zones, and those two choices explain almost every difference in the snippets above.

Native unitLanguages
SecondsPHP, C, Perl, Ruby, Bash
MillisecondsJavaScript, TypeScript, Java, Kotlin, Scala, Dart
Float secondsPython, R, Swift
Seconds + nanosecondsGo, Rust, C++ (chrono)
Something else entirelyC# (100 ns ticks from year 1), Excel (days from 1899)

The boundary between a millisecond language and a second language is where bugs cluster. A JavaScript front end talking to a PHP or Python back end crosses that boundary on every request, which is why seconds versus milliseconds is worth reading once, properly.