Timestamp Resources
Programming Language Timestamp Comparison
Compare Unix timestamp operations across JavaScript, Python, Java, Go, PHP, Ruby, C#, and Swift. Unless stated otherwise, values count seconds since 1970-01-01 00:00:00 UTC. Python examples assume import time and from datetime import datetime, timezone.
Current Timestamp
| Language | Seconds | Milliseconds | Microseconds |
|---|---|---|---|
| JavaScript | Math.floor(Date.now() / 1000) | Date.now() | — |
| Python | int(time.time()) | int(time.time() * 1000) | time.time_ns() // 1000 |
| Java | Instant.now().getEpochSecond() | Instant.now().toEpochMilli() | — |
| Go | time.Now().Unix() | time.Now().UnixMilli() | time.Now().UnixMicro() |
| PHP | time() | microtime(true) * 1000 | microtime(true) * 1000000 |
| Ruby | Time.now.to_i | (Time.now.to_r * 1000).floor | (Time.now.to_r * 1_000_000).floor |
| C# | DateTimeOffset.UtcNow.ToUnixTimeSeconds() | DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() | - |
| Swift | Date().timeIntervalSince1970 | Int(Date().timeIntervalSince1970 * 1000) | - |
These columns describe output units, not guaranteed clock resolution. JavaScript performance.now() measures elapsed time from a performance time origin; it is not a Unix timestamp. Read the clock once if multiple units must describe the same instant.
Timestamp to Date
| Language | Code |
|---|---|
| JavaScript | new Date(timestamp * 1000) |
| Python | datetime.fromtimestamp(timestamp, timezone.utc) |
| Java | Instant.ofEpochSecond(timestamp) |
| Go | time.Unix(timestamp, 0) |
| PHP | date('Y-m-d H:i:s', $timestamp) |
| Ruby | Time.at(timestamp) |
| C# | DateTimeOffset.FromUnixTimeSeconds(timestamp) |
| Swift | Date(timeIntervalSince1970: TimeInterval(timestamp)) |
Examples use Instant for Java and DateTimeOffset for C#. Swift requires import Foundation. PHP formatting uses the configured timezone; Swift Date values represent instants, and formatters apply timezones.
Date to Timestamp
| Language | Code |
|---|---|
| JavaScript | Math.floor(date.getTime() / 1000) |
| Python | math.floor(date.timestamp()) |
| Java | instant.getEpochSecond() (instant is an Instant) |
| Go | date.Unix() |
| PHP | strtotime('2024-01-01 00:00:00 UTC') |
| Ruby | date.to_i |
| C# | date.ToUnixTimeSeconds() |
| Swift | Int(floor(date.timeIntervalSince1970)) |
Python requires import math; use an aware datetime to avoid assuming the host timezone. Flooring preserves whole-second semantics for instants before 1970.
Formatting
| Language | Code |
|---|---|
| JavaScript | date.toISOString() or date.toLocaleString() |
| Python | date.strftime('%Y-%m-%d %H:%M:%S') |
| Java | DateTimeFormatter.ISO_INSTANT.format(instant) |
| Go | date.Format(time.RFC3339) |
| PHP | date('Y-m-d H:i:s', $timestamp) |
| Ruby | date.strftime('%Y-%m-%d %H:%M:%S') |
| C# | date.ToString("yyyy-MM-dd HH:mm:ss") |
| Swift | ISO8601DateFormatter().string(from: date) |
Timezone Handling
| Language | UTC | Local | Convert |
|---|---|---|---|
| JavaScript | new Date().toISOString() | new Date().toLocaleString() | date.toLocaleString('en-US', {timeZone: 'UTC'}) |
| Python | datetime.now(timezone.utc) | datetime.now().astimezone() | date.astimezone(timezone.utc) |
| Java | Instant.now() | LocalDateTime.now() | instant.atZone(ZoneId.of("UTC")) |
| Go | time.Now().UTC() | time.Now() | date.In(time.UTC) |
| PHP | gmdate('Y-m-d H:i:s') | date('Y-m-d H:i:s') | gmdate('Y-m-d H:i:s', $timestamp) |
| Ruby | Time.now.utc | Time.now | date.getutc |
| C# | DateTimeOffset.UtcNow | DateTimeOffset.Now | date.ToUniversalTime() |
| Swift | ISO8601DateFormatter().string(from: date) | DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .long) | formatter.timeZone = TimeZone(secondsFromGMT: 0) |
Key Differences
Epoch Start
The Unix APIs above use January 1, 1970 00:00:00 UTC. Other APIs can use different epochs. GPS starts at January 6, 1980 and requires a date-specific GPS−UTC offset; it is a separate time scale, not a programming-language exception.
Precision
- Seconds: Most common, 10 digits (until 2286)
- Milliseconds: 13 digits, JavaScript default
- Microseconds: 16 digits
- Nanoseconds: 19 digits, some systems
These digit counts describe present-day positive timestamps; the number of digits alone does not reliably identify the unit.
Timezone Storage
- JavaScript: Date stores epoch milliseconds without a timezone identifier; formatting chooses UTC or the host timezone
- Python: Naive vs Aware datetime objects
- Java: Instant (UTC) vs ZonedDateTime (with zone)
- Go: Location attached to Time object
- Ruby: Time object includes timezone offset
- C#: DateTimeOffset stores an instant and its offset; retain a timezone ID separately when needed
- Swift: Date represents an instant without a timezone; Calendar and formatters apply a zone
Best Practices
- Store instants consistently, usually in UTC; retain a named timezone separately for recurring local events
- Convert to local time only for display
- Use ISO 8601 for API serialization
- Handle leap seconds carefully (GPS/TAI vs UTC)
- Consider precision needed for your use case
Check a sample value
Unix second 1704067200 represents 2024-01-01 00:00:00 UTC. Check it in the epoch seconds and date converter, then compare 1704067200000 in the millisecond timestamp converter. Digit count is only a hint for recent dates; specify the unit in an API contract.
Use timezone-aware Python datetime objects. datetime.utcnow() is deprecated since Python 3.12; Python's documentation recommends datetime.now(timezone.utc).