Programming Language Timestamp Comparison
Quick reference for timestamp operations across popular programming languages.
Current Timestamp
| Language | Seconds | Milliseconds | Microseconds |
|---|---|---|---|
| JavaScript | Math.floor(Date.now() / 1000) | Date.now() | performance.now() |
| Python | int(time.time()) | int(time.time() * 1000) | time.time_ns() // 1000 |
| Java | System.currentTimeMillis() / 1000 | System.currentTimeMillis() | Instant.now().getEpochSecond() |
| 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_f * 1000 | Time.now.to_r |
| C# | DateTimeOffset.UtcNow.ToUnixTimeSeconds() | DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() | - |
| Swift | Date().timeIntervalSince1970 | Int(Date().timeIntervalSince1970 * 1000) | - |
Timestamp to Date
| Language | Code |
|---|---|
| JavaScript | new Date(timestamp * 1000) |
| Python | datetime.fromtimestamp(timestamp) |
| 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)) |
Date to Timestamp
| Language | Code |
|---|---|
| JavaScript | Math.floor(date.getTime() / 1000) |
| Python | int(date.timestamp()) |
| Java | date.toEpochSecond(ZoneOffset.UTC) |
| Go | date.Unix() |
| PHP | strtotime('2024-01-01') |
| Ruby | date.to_i |
| C# | ((DateTimeOffset)date).ToUnixTimeSeconds() |
| Swift | Int(date.timeIntervalSince1970) |
Formatting
| Language | Code |
|---|---|
| JavaScript | date.toISOString() or date.toLocaleString() |
| Python | date.strftime('%Y-%m-%d %H:%M:%S') |
| Java | DateTimeFormatter.ISO_DATE_TIME.format(date) |
| 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 | dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" |
Timezone Handling
| Language | UTC | Local | Convert |
|---|---|---|---|
| JavaScript | new Date().toISOString() | new Date().toLocaleString() | date.toLocaleString('en-US', {timeZone: 'UTC'}) |
| Python | datetime.utcnow() | datetime.now() | date.astimezone(timezone('UTC')) |
| Java | Instant.now() | LocalDateTime.now() | date.atZone(ZoneId.of("UTC")) |
| Go | time.Now().UTC() | time.Now() | date.In(time.UTC) |
| PHP | gmdate() | date() | date_default_timezone_set('UTC') |
| Ruby | Time.now.utc | Time.now | Time.now.getutc |
| C# | DateTime.UtcNow | DateTime.Now | TimeZoneInfo.ConvertTimeToUtc(date) |
| Swift | Date() (always UTC internally) | Date() | Calendar.current.dateComponents |
Key Differences
Epoch Start
- All languages use Unix epoch: January 1, 1970 00:00:00 UTC
- Exception: GPS time uses January 6, 1980
- TAI uses January 1, 1958
Precision
- Seconds: Most common, 10 digits (until 2286)
- Milliseconds: 13 digits, JavaScript default
- Microseconds: 16 digits
- Nanoseconds: 19 digits, some systems
Timezone Storage
- JavaScript: No timezone in Date object, uses browser 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#: DateTimeKind (Unspecified, Utc, Local)
- Swift: Date is always UTC, Calendar handles zones
Best Practices
- Always store UTC in databases
- 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