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.
-
Python
Native unit: float seconds
-
JavaScript
Native unit: integer milliseconds
-
PHP
Native unit: integer seconds
-
Java
Native unit: integer milliseconds
-
Go
Native unit: integer seconds and nanoseconds
-
TypeScript
Native unit: integer milliseconds
-
Ruby
Native unit: float seconds
-
C#
Native unit: ticks (100 ns)
-
Rust
Native unit: seconds + nanoseconds
-
SQL
Native unit: varies by engine
-
Bash
Native unit: integer seconds
-
C++
Native unit: chrono duration
-
Swift
Native unit: float seconds
-
Kotlin
Native unit: integer milliseconds
-
C
Native unit: time_t seconds
-
Perl
Native unit: integer seconds
-
Dart
Native unit: integer milliseconds
-
PowerShell
Native unit: .NET DateTimeOffset
-
Scala
Native unit: integer milliseconds
-
R
Native unit: float seconds
-
Excel
Native unit: days since 1899-12-30
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 unit | Languages |
|---|---|
| Seconds | PHP, C, Perl, Ruby, Bash |
| Milliseconds | JavaScript, TypeScript, Java, Kotlin, Scala, Dart |
| Float seconds | Python, R, Swift |
| Seconds + nanoseconds | Go, Rust, C++ (chrono) |
| Something else entirely | C# (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.