Timestamp Resources

Programming Language Timestamp Comparison

Programming Language Timestamp Comparison

Quick reference for timestamp operations across popular programming languages.

Current Timestamp

LanguageSecondsMillisecondsMicroseconds
JavaScriptMath.floor(Date.now() / 1000)Date.now()performance.now()
Pythonint(time.time())int(time.time() * 1000)time.time_ns() // 1000
JavaSystem.currentTimeMillis() / 1000System.currentTimeMillis()Instant.now().getEpochSecond()
Gotime.Now().Unix()time.Now().UnixMilli()time.Now().UnixMicro()
PHPtime()microtime(true) * 1000microtime(true) * 1000000
RubyTime.now.to_iTime.now.to_f * 1000Time.now.to_r
C#DateTimeOffset.UtcNow.ToUnixTimeSeconds()DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()-
SwiftDate().timeIntervalSince1970Int(Date().timeIntervalSince1970 * 1000)-

Timestamp to Date

LanguageCode
JavaScriptnew Date(timestamp * 1000)
Pythondatetime.fromtimestamp(timestamp)
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))

Date to Timestamp

LanguageCode
JavaScriptMath.floor(date.getTime() / 1000)
Pythonint(date.timestamp())
Javadate.toEpochSecond(ZoneOffset.UTC)
Godate.Unix()
PHPstrtotime('2024-01-01')
Rubydate.to_i
C#((DateTimeOffset)date).ToUnixTimeSeconds()
SwiftInt(date.timeIntervalSince1970)

Formatting

LanguageCode
JavaScriptdate.toISOString() or date.toLocaleString()
Pythondate.strftime('%Y-%m-%d %H:%M:%S')
JavaDateTimeFormatter.ISO_DATE_TIME.format(date)
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")
SwiftdateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

Timezone Handling

LanguageUTCLocalConvert
JavaScriptnew Date().toISOString()new Date().toLocaleString()date.toLocaleString('en-US', {timeZone: 'UTC'})
Pythondatetime.utcnow()datetime.now()date.astimezone(timezone('UTC'))
JavaInstant.now()LocalDateTime.now()date.atZone(ZoneId.of("UTC"))
Gotime.Now().UTC()time.Now()date.In(time.UTC)
PHPgmdate()date()date_default_timezone_set('UTC')
RubyTime.now.utcTime.nowTime.now.getutc
C#DateTime.UtcNowDateTime.NowTimeZoneInfo.ConvertTimeToUtc(date)
SwiftDate() (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

  1. Always store UTC in databases
  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

Related Resources