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

LanguageSecondsMillisecondsMicroseconds
JavaScriptMath.floor(Date.now() / 1000)Date.now()—
Pythonint(time.time())int(time.time() * 1000)time.time_ns() // 1000
JavaInstant.now().getEpochSecond()Instant.now().toEpochMilli()—
Gotime.Now().Unix()time.Now().UnixMilli()time.Now().UnixMicro()
PHPtime()microtime(true) * 1000microtime(true) * 1000000
RubyTime.now.to_i(Time.now.to_r * 1000).floor(Time.now.to_r * 1_000_000).floor
C#DateTimeOffset.UtcNow.ToUnixTimeSeconds()DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()-
SwiftDate().timeIntervalSince1970Int(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

LanguageCode
JavaScriptnew Date(timestamp * 1000)
Pythondatetime.fromtimestamp(timestamp, timezone.utc)
JavaInstant.ofEpochSecond(timestamp)
Gotime.Unix(timestamp, 0)
PHPdate('Y-m-d H:i:s', $timestamp)
RubyTime.at(timestamp)
C#DateTimeOffset.FromUnixTimeSeconds(timestamp)
SwiftDate(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

LanguageCode
JavaScriptMath.floor(date.getTime() / 1000)
Pythonmath.floor(date.timestamp())
Javainstant.getEpochSecond() (instant is an Instant)
Godate.Unix()
PHPstrtotime('2024-01-01 00:00:00 UTC')
Rubydate.to_i
C#date.ToUnixTimeSeconds()
SwiftInt(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

LanguageCode
JavaScriptdate.toISOString() or date.toLocaleString()
Pythondate.strftime('%Y-%m-%d %H:%M:%S')
JavaDateTimeFormatter.ISO_INSTANT.format(instant)
Godate.Format(time.RFC3339)
PHPdate('Y-m-d H:i:s', $timestamp)
Rubydate.strftime('%Y-%m-%d %H:%M:%S')
C#date.ToString("yyyy-MM-dd HH:mm:ss")
SwiftISO8601DateFormatter().string(from: date)

Timezone Handling

LanguageUTCLocalConvert
JavaScriptnew Date().toISOString()new Date().toLocaleString()date.toLocaleString('en-US', {timeZone: 'UTC'})
Pythondatetime.now(timezone.utc)datetime.now().astimezone()date.astimezone(timezone.utc)
JavaInstant.now()LocalDateTime.now()instant.atZone(ZoneId.of("UTC"))
Gotime.Now().UTC()time.Now()date.In(time.UTC)
PHPgmdate('Y-m-d H:i:s')date('Y-m-d H:i:s')gmdate('Y-m-d H:i:s', $timestamp)
RubyTime.now.utcTime.nowdate.getutc
C#DateTimeOffset.UtcNowDateTimeOffset.Nowdate.ToUniversalTime()
SwiftISO8601DateFormatter().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

  1. Store instants consistently, usually in UTC; retain a named timezone separately for recurring local events
  2. Convert to local time only for display
  3. Use ISO 8601 for API serialization
  4. Handle leap seconds carefully (GPS/TAI vs UTC)
  5. 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).

Related Resources