Unix timestamps in every language
Copy-ready snippets for getting the current epoch time and converting between timestamps and dates — with the gotchas that bite each language.
JavaScript
In JavaScript, Date.now() returns the current time in milliseconds since the Unix epoch. Divide by 1000 for the seconds value most backends expect, and use new Date(ms) to turn a timestamp back into a date.
TypeScript
TypeScript uses the same runtime APIs as JavaScript — Date.now() for milliseconds — but lets you type timestamps explicitly (e.g. number) and model branded epoch types for safety.
Python
Python's time.time() returns the current Unix time as a float of seconds, while datetime.fromtimestamp(ts, tz=timezone.utc) converts a timestamp to an aware datetime. Always pass a tz to avoid local-time surprises.
Go
In Go, time.Now().Unix() returns the current Unix time in seconds, and time.Unix(sec, nsec) builds a time.Time from a timestamp. time.Now().UnixNano() gives nanosecond precision.
Rust
In Rust, SystemTime::now().duration_since(UNIX_EPOCH) yields the time since the epoch as a Duration; call .as_secs() or .as_millis(). The chrono crate offers richer formatting via DateTime::from_timestamp.
Java
Modern Java uses java.time: Instant.now().getEpochSecond() for the current second, and Instant.ofEpochSecond(ts) to rebuild an instant. System.currentTimeMillis() remains for milliseconds.
Kotlin
Kotlin on the JVM uses java.time directly: Instant.now().epochSecond for the current second and Instant.ofEpochSecond(ts) to convert back. System.currentTimeMillis() returns milliseconds.
Swift
In Swift, Date().timeIntervalSince1970 returns the current Unix time as a Double of seconds, and Date(timeIntervalSince1970: ts) converts a timestamp back to a Date.
PHP
PHP's time() returns the current Unix timestamp in seconds, and gmdate('c', $ts) formats a timestamp as an ISO 8601 string in UTC. DateTime::createFromFormat('U', $ts) builds an object.
Ruby
In Ruby, Time.now.to_i returns the current Unix time in seconds, and Time.at(ts) builds a Time from a timestamp. Call .utc to render it in UTC.
C#
In C#, DateTimeOffset.UtcNow.ToUnixTimeSeconds() returns the current Unix time, and DateTimeOffset.FromUnixTimeSeconds(ts) converts a timestamp back. Both keep the UTC offset explicit.
SQL
Databases differ: PostgreSQL uses to_timestamp(ts) and extract(epoch from now()), while MySQL uses FROM_UNIXTIME(ts) and UNIX_TIMESTAMP(). Both convert between epoch integers and native timestamp types.
Bash
In the shell, date +%s prints the current Unix timestamp in seconds. GNU date converts with date -d @ts; macOS/BSD date uses date -r ts.
PowerShell
PowerShell can use .NET directly: [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() for the current timestamp and [DateTimeOffset]::FromUnixTimeSeconds(ts) to convert back.