← All posts

Current Unix Timestamp: Get Epoch Time Now in Code

The current Unix timestamp is now counted from 1970-01-01 UTC. Use seconds for Unix tools, SQL filters, JWT exp claims, and many server APIs; use milliseconds for JavaScript Date, Java currentTimeMillis, browser analytics, and APIs that document 13-digit values. For durations, use a monotonic clock instead of wall-clock now.

What 'current Unix timestamp' means in real code

The current Unix timestamp is the number that represents this instant, counted from 1970-01-01 00:00:00 UTC. The part that causes bugs is not the epoch. It is the unit. A 10-digit value such as 1750409100 is seconds. A 13-digit value such as 1750409100000 is milliseconds. Both can describe the same instant, but they are not interchangeable.

Before you copy a one-liner, ask yourself three things:

  • Who will read the value? A Unix tool, SQL database, JWT claim, or server API usually wants seconds.
  • Is the value going into JavaScript Date, Java currentTimeMillis, browser analytics, or a frontend event stream? Use milliseconds.
  • Are you measuring how long code took to run? Do not use Unix time. Use a monotonic clock such as performance.now(), time.monotonic(), time.Since(), Stopwatch, or hrtime.

Timezone does not change the timestamp. A laptop in Los Angeles and a server in Frankfurt should return the same Unix timestamp at the same instant. Timezone only matters later, when you turn the number into a human-readable date.

Current timestamp in seconds

Use seconds when the receiving system documents Unix time, epoch time, or POSIX time without mentioning milliseconds. This is the safe default for Linux shell commands, PHP time(), Python int(time.time()), PostgreSQL epoch filters, MySQL UNIX_TIMESTAMP(), and JWT exp / iat claims.

  • Modern shape: 10 digits
  • Example field names: created_at_seconds, expiresAtSeconds, issued_at_epoch
  • Common mistake: passing a 10-digit seconds value into new Date() and getting a date in 1970

Current timestamp in milliseconds

Use milliseconds when you are working inside JavaScript Date, Java System.currentTimeMillis(), .NET ToUnixTimeMilliseconds(), browser events, frontend logs, or APIs that show 13-digit examples. Milliseconds are convenient in UI code because the value can go straight into a Date constructor.

  • Modern shape: 13 digits
  • Example field names: createdAtMs, clientSentAtMs, event_time_ms
  • Common mistake: sending a 13-digit milliseconds value to a seconds-based API and getting a date thousands of years in the future

Current timestamp is UTC-based

Unix timestamp math is UTC-based even when the machine displays local time. If your server stores 1750409100, the integer is the same in UTC, New York, Tokyo, and Berlin. Only the formatted string changes: 2025-06-20 09:25 UTC, 05:25 in New York during daylight saving time, or 18:25 in Tokyo.

Get the current Unix timestamp in JavaScript

JavaScript's Date.now() returns Unix milliseconds. That is perfect when the value stays inside browser or Node.js Date code. If you are preparing a payload for a seconds-based API, convert at the boundary and make the unit obvious in the variable name.

  • const createdAtMs = Date.now() // 1750409100000, Unix milliseconds
  • const createdAtSeconds = Math.floor(Date.now() / 1000) // 1750409100, Unix seconds
  • new Date(createdAtMs) // direct, because Date expects milliseconds
  • new Date(createdAtSeconds * 1000) // convert seconds before constructing Date
  • const started = performance.now(); doWork(); const elapsedMs = performance.now() - started // elapsed time
  • Node.js: Date.now() works the same as the browser; use node:perf_hooks performance.now() for elapsed timings

Do not use Date.now() - start for benchmarks, retry timers, or latency measurements. NTP can adjust the wall clock while your code is running, which can make wall-clock durations jump. Use performance.now() for duration and Date.now() for "when did this event happen?"

JavaScript now in seconds

Use Math.floor(Date.now() / 1000) for JWT exp claims, Unix-second API fields, and database rows that store seconds. Avoid Date.now() / 1000 without Math.floor unless the target accepts a floating-point value.

JavaScript now in milliseconds

Use Date.now() directly for UI event timestamps, local storage records, browser analytics, and anything that will become a JavaScript Date without crossing a seconds-based API boundary.

Get the current Unix timestamp in Python

Python's time.time() returns Unix seconds as a float. That is useful when fractional seconds matter, but many APIs and database columns expect an integer. Cast with int() for current Unix seconds, multiply before casting for milliseconds, and use timezone-aware datetime objects when you need a readable UTC value.

  • import time
  • now_seconds_float = time.time() # 1750409100.123, fractional Unix seconds
  • now_seconds = int(time.time()) # 1750409100, integer Unix seconds
  • now_ms = int(time.time() * 1000) # 1750409100123, Unix milliseconds
  • now_ns = time.time_ns() # 1750409100123456789, Unix nanoseconds, Python 3.7+
  • from datetime import datetime, timezone
  • datetime.now(timezone.utc).timestamp() # Unix seconds from an explicit UTC datetime
  • elapsed = time.monotonic() # monotonic seconds for durations, not Unix time

The important habit is passing timezone.utc. datetime.now().timestamp() uses the machine's local timezone assumptions, which makes laptop-vs-server differences harder to debug.

Get the current Unix timestamp in Go

Go makes the unit explicit. time.Now().Unix() gives seconds; UnixMilli(), UnixMicro(), and UnixNano() give progressively finer units. For most API payloads and log fields, pick one unit at the edge of the system and keep the rest of the codebase consistent.

  • now := time.Now()
  • now.Unix() // 1750409100, Unix seconds
  • now.UnixMilli() // 1750409100123, Unix milliseconds, Go 1.17+
  • now.UnixMicro() // microseconds, Go 1.17+
  • now.UnixNano() // nanoseconds
  • now.UTC() // changes display/location, not the Unix timestamp
  • start := time.Now(); doWork(); elapsed := time.Since(start) // monotonic elapsed time

Go's time.Time may carry monotonic clock data inside the process, which is why time.Since(start) is the right duration pattern. Once you serialize the value to JSON, SQL, or a Unix number, only the wall-clock instant remains.

Get the current Unix timestamp in Java and Kotlin

For Java 8+ code, Instant is the clearest API. Use getEpochSecond() when the target wants Unix seconds, and toEpochMilli() when it wants JavaScript-style milliseconds. System.currentTimeMillis() is still common and perfectly fine for millisecond wall-clock timestamps, but it is not an elapsed-time clock.

  • Instant.now().getEpochSecond() // long, Unix seconds
  • Instant.now().toEpochMilli() // long, Unix milliseconds
  • System.currentTimeMillis() // long, Unix milliseconds
  • System.nanoTime() // monotonic nanoseconds for elapsed timing, not Unix time
  • Kotlin (JVM): Instant.now().toEpochMilli() // same java.time API
  • Kotlin Multiplatform: Clock.System.now().toEpochMilliseconds()

Use seconds for JWT exp, many REST APIs, and Unix-style database fields. Use milliseconds for Android event timestamps, JavaScript interop, and JVM systems that already document currentTimeMillis-style values.

Get the current Unix timestamp in C# / .NET

.NET gives dedicated Unix converters on DateTimeOffset. Prefer those over hand-rolled DateTime math. The common trap is DateTime.UtcNow.Ticks: ticks are 100-nanosecond intervals since year 0001, not since 1970, so they are not a Unix timestamp.

  • DateTimeOffset.UtcNow.ToUnixTimeSeconds() // long, Unix seconds
  • DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() // long, Unix milliseconds
  • DateTimeOffset.UtcNow.Ticks // 100-ns ticks since 0001-01-01, not Unix time
  • Stopwatch.GetTimestamp() // monotonic high-resolution timer for durations
  • Stopwatch.Frequency // ticks per second on this system
  • .NET Framework < 4.6: (DateTime.UtcNow - new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)).TotalSeconds

Get the current Unix timestamp in PHP

PHP defaults to Unix seconds, which makes time() one of the shortest correct answers in this whole guide. Reach for microtime(true) only when you actually need sub-second precision, and be careful not to store its float form in a column that expects an integer.

  • time() // 1750409100, Unix seconds
  • microtime(true) // 1750409100.123456, Unix seconds with microseconds as a float
  • (int) floor(microtime(true) * 1000) // 1750409100123, Unix milliseconds
  • (new DateTimeImmutable('now', new DateTimeZone('UTC')))->getTimestamp() // Unix seconds
  • (new DateTimeImmutable())->format('U') // "1750409100", Unix seconds as a string
  • hrtime(true) // monotonic nanoseconds, PHP 7.3+, for durations

For API fields, decide whether the consumer expects seconds or milliseconds before multiplying. PHP's native time() value is seconds; JavaScript clients usually expect milliseconds.

Get the current Unix timestamp in Ruby, Swift, Rust, and C

These languages use the same Unix epoch but expose it through different types. The specific return type matters in real code: Swift returns a Double, Rust returns a Duration, and C's time_t width depends on the platform.

  • Ruby: Time.now.to_i // Integer, Unix seconds
  • Ruby: (Time.now.to_f * 1000).to_i // Integer, Unix milliseconds
  • Swift: Int(Date().timeIntervalSince1970) // Int, Unix seconds
  • Swift: Int(Date().timeIntervalSince1970 * 1000) // Int, Unix milliseconds
  • Rust: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() // u64 seconds
  • Rust: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() // u128 milliseconds
  • C: time(NULL) // time_t, Unix seconds
  • C: clock_gettime(CLOCK_REALTIME, &ts) // wall-clock seconds plus nanoseconds
  • C: clock_gettime(CLOCK_MONOTONIC, &ts) // monotonic elapsed-time clock

If you are writing C for older 32-bit systems, check the size and signedness of time_t. A 32-bit signed time_t cannot represent dates after 2038-01-19.

Get the current Unix timestamp in shell

Shell commands are handy for logs, scripts, curl payloads, and quick checks while debugging. Linux and macOS both support date +%s for seconds. Milliseconds are where they differ: GNU date supports %N, while macOS BSD date does not.

  • Linux / macOS seconds: date +%s
  • Linux milliseconds: date +%s%3N // GNU date only
  • Linux nanoseconds: date +%s%N // GNU date only
  • macOS milliseconds with Python: python3 -c 'import time; print(int(time.time() * 1000))'
  • macOS milliseconds with Node.js: node -e 'console.log(Date.now())'
  • PowerShell seconds: [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
  • PowerShell milliseconds: [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

Do not use awk srand() as a portable timestamp. Some awk implementations return the previous seed from srand(), not the current time, so the result is not reliable enough for scripts.

Linux current epoch seconds

date +%s is the standard Linux answer for current epoch seconds and works on macOS as well.

Linux current epoch milliseconds

Use date +%s%3N on GNU date. On macOS, use Python or Node for milliseconds unless you have installed GNU coreutils and call gdate.

Get the current Unix timestamp in SQL

SQL functions read the database server's clock, not the application server's clock. That distinction matters when you insert audit rows, compare against NOW(), or run multi-region jobs. PostgreSQL also has a transaction-time detail: NOW() is fixed at the start of the transaction, while clock_timestamp() returns the actual wall clock at the moment the function runs.

  • PostgreSQL seconds: SELECT EXTRACT(EPOCH FROM NOW())::BIGINT
  • PostgreSQL current wall clock: SELECT EXTRACT(EPOCH FROM clock_timestamp())
  • PostgreSQL milliseconds: SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::BIGINT
  • MySQL seconds: SELECT UNIX_TIMESTAMP()
  • MySQL milliseconds: SELECT FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)
  • SQLite seconds: SELECT strftime('%s', 'now')
  • SQLite milliseconds: SELECT CAST((julianday('now') - 2440587.5) * 86400000 AS INTEGER)
  • BigQuery seconds: SELECT UNIX_SECONDS(CURRENT_TIMESTAMP())
  • ClickHouse seconds: SELECT toUnixTimestamp(now())

For storage, a native timestamp type is usually easier to query than a raw integer. Store raw epoch integers when you need compatibility with an external protocol or a very compact event stream.

PostgreSQL current epoch time

EXTRACT(EPOCH FROM NOW()) returns the transaction timestamp. Use clock_timestamp() when a long-running transaction needs the actual current wall clock.

MySQL and SQLite current timestamp

MySQL's UNIX_TIMESTAMP() and SQLite's strftime('%s', 'now') both return current Unix seconds. Add explicit FLOOR or CAST when you compute milliseconds.

Clock drift, NTP, and why 'current' is not perfectly identical between hosts

Two servers asked for the current timestamp at the same physical instant should be close, but not always identical. Real clocks drift. Virtual machines can pause. Containers read the host clock. Operating systems correct the wall clock with NTP or a similar time-sync service. That is why current timestamps are good for event ordering at human scale, but not a perfect distributed-system ordering primitive.

  • Healthy NTP-synced servers are often within a few milliseconds of true UTC, but verify your own fleet before depending on that number.
  • Unsynced hosts can drift seconds per day, which is enough to break token expiry, cache invalidation, and log correlation.
  • Containers do not have an independent clock; they read the host clock.
  • For elapsed time inside one process, use monotonic time, not Unix time.
  • For high-precision clock sync on a LAN, PTP (IEEE 1588) can reach microsecond-level synchronization when the hardware and network support it.
  • Practical checks: chronyc tracking on Chrony hosts, timedatectl timesync-status on many Linux systems, w32tm /query /status on Windows.

Quick reference — current timestamp in every language

Use this section when reviewing a PR or copying a one-liner into a script. The value is always anchored to 1970-01-01 UTC; the column you care about is the unit.

  • JavaScript: Date.now() // milliseconds
  • JavaScript seconds: Math.floor(Date.now() / 1000)
  • Python: int(time.time()) // seconds
  • Python milliseconds: int(time.time() * 1000)
  • Go: time.Now().Unix() // seconds
  • Go milliseconds: time.Now().UnixMilli()
  • Java: Instant.now().getEpochSecond() // seconds
  • Java milliseconds: Instant.now().toEpochMilli()
  • Kotlin: Clock.System.now().toEpochMilliseconds() // milliseconds
  • C#: DateTimeOffset.UtcNow.ToUnixTimeSeconds() // seconds
  • PHP: time() // seconds
  • Ruby: Time.now.to_i // seconds
  • Swift: Int(Date().timeIntervalSince1970) // seconds
  • Rust: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() // seconds
  • C: time(NULL) // seconds
  • Linux shell: date +%s // seconds
  • PowerShell: [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() // seconds
  • PostgreSQL: SELECT EXTRACT(EPOCH FROM NOW())::BIGINT // seconds
  • MySQL: SELECT UNIX_TIMESTAMP() // seconds

The safest naming convention is still the simplest one: put the unit in the field name. createdAtMs and expiresAtSeconds are boring, but they prevent the next person from counting digits in a debugger.

FAQ

What is epoch time now?
Epoch time now is the current Unix timestamp: the number of seconds since 1970-01-01 00:00:00 UTC at this instant. The homepage shows the live value; code usually reads it with date +%s, Math.floor(Date.now() / 1000), or int(time.time()).
What's the simplest way to get the current Unix timestamp?
Pick the unit first. For Unix seconds: date +%s in shell, Math.floor(Date.now() / 1000) in JavaScript, int(time.time()) in Python, time.Now().Unix() in Go, or Instant.now().getEpochSecond() in Java. For milliseconds: Date.now(), System.currentTimeMillis(), or DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().
Does the current Unix timestamp depend on my timezone?
No. A Unix timestamp is a timezone-neutral integer — the count of seconds since 1970-01-01 UTC. Two computers in different timezones running 'get current timestamp' at the same instant return the same number. Timezone only matters when you format the number for display.
Why is my current timestamp 13 digits and not 10?
Because your runtime returns milliseconds rather than seconds. JavaScript Date.now(), Java System.currentTimeMillis(), and .NET DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() all return milliseconds. Divide by 1000 (and floor) to get 10-digit Unix seconds.
Is the current Unix timestamp the same on every server?
It should be close if the servers are synchronized, but do not assume perfect equality. NTP, virtualization pauses, host-clock drift, and manual clock changes can create small differences. Use Unix time for event timestamps and logs; use a monotonic clock for elapsed-time measurements.
How accurate is Date.now() / time.time() / Instant.now()?
Precision and accuracy are different. Date.now() reports integer milliseconds, time.time() reports fractional seconds, and Instant.now() can expose finer precision depending on the clock. Accuracy depends on the host's time sync. For benchmarks, retries, and timeouts, use performance.now(), time.monotonic(), time.Since(), Stopwatch, or hrtime.
What's the difference between wall-clock and monotonic time?
Wall-clock time is the system's belief about UTC — it can jump forward or backward when NTP adjusts the clock. Monotonic time is a counter that only moves forward, with no relationship to UTC. Use wall-clock for 'when did this happen' (timestamps, logs). Use monotonic for 'how long did it take' (benchmarks, timeouts, rate limiters).
How do I get the current timestamp in milliseconds in every language?
JavaScript: Date.now(). Java: System.currentTimeMillis(). C#: DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(). Python: int(time.time() * 1000). Go: time.Now().UnixMilli(). Rust: as_millis() from UNIX_EPOCH. Linux with GNU date: date +%s%3N. macOS: use Python or Node because BSD date does not support %N.
How do I get current epoch time in SQL?
PostgreSQL: SELECT EXTRACT(EPOCH FROM NOW())::BIGINT for transaction time, or clock_timestamp() for the actual wall clock during a long transaction. MySQL: SELECT UNIX_TIMESTAMP(). SQLite: SELECT strftime('%s', 'now'). BigQuery: SELECT UNIX_SECONDS(CURRENT_TIMESTAMP()).
Why does the current timestamp differ between my computer and a server?
Usually because one machine's wall clock has drifted from UTC or was adjusted by NTP. Other common causes are VM pauses, container hosts with bad time sync, manual clock changes, and rarely hardware clock problems. Timezone settings change display, not the Unix timestamp itself.
Should I store the current timestamp as seconds or milliseconds?
Match the system boundary. If JavaScript or Java is the writer, milliseconds usually fit the native runtime. If Unix tools, Python, PHP, Go, SQL, or JWT claims are the boundary, seconds are often expected. Either way, put the unit in the name: createdAtMs, created_at_seconds, expiresAtSeconds.