How to Get the Current Epoch Time

Every language can produce the current Unix timestamp in a line. The trap is that they do not agree on the unit: some return seconds, some return milliseconds, and a few return floats. The snippets below are grouped by what they actually return.

Languages that return seconds

  • Python: int(time.time()) — time.time() itself returns a float with sub-second precision.
  • PHP: time()
  • Ruby: Time.now.to_i
  • Go: time.Now().Unix()
  • Rust: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
  • bash: date +%s
  • PostgreSQL: SELECT EXTRACT(EPOCH FROM NOW())::bigint
  • MySQL: SELECT UNIX_TIMESTAMP()

Languages that return milliseconds

  • JavaScript and TypeScript: Date.now() — divide by 1000 and floor for seconds.
  • Java: System.currentTimeMillis(), or Instant.now().getEpochSecond() for seconds.
  • Kotlin: System.currentTimeMillis()
  • C#: DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), or ToUnixTimeSeconds() for seconds.
  • Swift: Date().timeIntervalSince1970 returns a Double in seconds, so multiply for milliseconds.

Converting between the two

Going from milliseconds to seconds, always floor rather than round: rounding can push a timestamp into the next second and break comparisons that assume monotonic ordering. Going the other way, multiply by exactly 1000 rather than reformatting through a date string, which risks a time-zone conversion you did not intend.

Excel and spreadsheets

Spreadsheets store dates as days since 1900, not seconds since 1970. To turn a Unix timestamp in cell A1 into a readable date: =(A1/86400)+DATE(1970,1,1), then format the result as a date. To go the other way: =(A1-DATE(1970,1,1))*86400.

A note on clocks

All of the above read the system wall clock, which can jump backwards when NTP corrects it or when a user changes the time. For measuring how long something took, use your platform's monotonic clock instead — time.monotonic() in Python, performance.now() in JavaScript, System.nanoTime() in Java. Use epoch time to record when something happened, not to measure how long it took.

Frequently asked questions

How do I get the current epoch time in Python?

Use int(time.time()) for whole seconds. time.time() on its own returns a float with sub-second precision.

How do I get the current Unix timestamp in JavaScript?

Date.now() returns milliseconds. For seconds, use Math.floor(Date.now() / 1000).

How do I get epoch time in bash?

Run date +%s for seconds, or date +%s%3N on GNU coreutils for milliseconds.

How do I convert a Unix timestamp in Excel?

With the timestamp in A1, use =(A1/86400)+DATE(1970,1,1) and format the cell as a date. To convert a date back, use =(A1-DATE(1970,1,1))*86400.

Should I use epoch time to measure elapsed time?

No. The system wall clock can jump when NTP corrects it. Use a monotonic clock — time.monotonic(), performance.now() or System.nanoTime() — for durations, and epoch time for recording when an event happened.