Python
Programming
Time Measurement
Milliseconds
Coding Tips

How to get the current time in milliseconds in Python

Codemia Knowledge Hub ·

Use int(time.time() * 1000). It gives a Unix timestamp in milliseconds, the same format as JavaScript's Date.now(), Java's System.currentTimeMillis() and most APIs that accept millisecond timestamps.

time.time() for timestamps

python
1import time
2
3millis = int(time.time() * 1000)
4print(millis)  # e.g., 1718700000000

time.time() returns seconds since the Unix epoch (January 1, 1970 00:00:00 UTC) as a float. Multiplying by 1000 converts to milliseconds, and int() truncates the fraction.

Use it for logging, API calls, database records, or anywhere you need wall-clock time as an integer.

round() is not needed

Some guides suggest int(round(time.time() * 1000)). Truncation and rounding differ by at most 1 millisecond, and time.time() itself has limited precision (platform-dependent, typically microseconds), so rounding does not improve accuracy.

datetime.timestamp() when you already have a datetime

python
1from datetime import datetime
2
3now = datetime.now()
4millis = int(now.timestamp() * 1000)
5print(millis)  # e.g., 1718700000000

This gives the same result as time.time() via a datetime object. It is useful for converting an existing datetime:

python
1from datetime import datetime
2
3# Convert a specific datetime to milliseconds
4event_time = datetime(2026, 6, 18, 14, 30, 0)
5event_millis = int(event_time.timestamp() * 1000)

Do not create a datetime just to get the current time in milliseconds. Call time.time() directly.

time.time_ns() to avoid float precision loss (Python 3.7+)

python
1import time
2
3millis = time.time_ns() // 1_000_000
4print(millis)  # e.g., 1718700000000

time.time_ns() returns nanoseconds since the epoch as an integer, so there is no floating-point precision loss. Integer division by 1,000,000 gives milliseconds. Use it when rounding artifacts matter, such as in financial systems or when comparing timestamps that are very close together.

time.perf_counter() for elapsed time

python
1import time
2
3start = time.perf_counter()
4# ... operation you want to time ...
5elapsed_ms = (time.perf_counter() - start) * 1000
6print(f"Took {elapsed_ms:.2f} ms")

perf_counter() is the highest-resolution timer on your platform for measuring short durations. It does not return wall-clock time or a Unix timestamp, and its value has no defined relationship to any calendar epoch. Use it for benchmarking and profiling, never for timestamps.

time.monotonic() for timeouts

python
1import time
2
3deadline = time.monotonic() + 5.0  # 5 seconds from now
4
5while time.monotonic() < deadline:
6    # do work
7    pass

time.monotonic() never goes backward, even if the system clock is adjusted (NTP sync, manual change, DST). Like perf_counter(), it is not wall-clock time. Use it for timeouts, retry delays and other logic that must be immune to system time changes.

MethodReturnsEpoch-BasedPrecisionUse Case
time.time() * 1000floatYesMicroseconds (platform-dependent)Timestamps, logging, APIs
datetime.now().timestamp() * 1000floatYesMicrosecondsConverting datetime objects
time.time_ns() // 1_000_000intYesNanosecondsHigh-precision timestamps
time.perf_counter() * 1000floatNoSub-microsecondBenchmarking, profiling
time.monotonic() * 1000floatNoSub-microsecondTimeouts, scheduling

Epoch milliseconds do not depend on timezone

time.time() always returns UTC-based epoch time. A timezone-aware datetime gives the same epoch value regardless of offset:

python
1from datetime import datetime, timezone, timedelta
2
3# Current time in UTC (same as time.time())
4now = datetime.now(timezone.utc)
5utc_millis = int(now.timestamp() * 1000)
6
7# The same instant in a specific offset
8est = timezone(timedelta(hours=-5))
9est_now = now.astimezone(est)
10est_millis = int(est_now.timestamp() * 1000)
11
12# Both produce the same epoch milliseconds -- timezone only affects display
13print(utc_millis == est_millis)  # True

The number of milliseconds since 1970-01-01T00:00:00Z is the same in every timezone. Timezones only matter when you format the timestamp as a human-readable string.

Equivalents in other languages

python
# Python
import time
millis = int(time.time() * 1000)
javascript
// JavaScript
const millis = Date.now();
java
// Java
long millis = System.currentTimeMillis();
bash
# Bash (GNU coreutils)
millis=$(date +%s%3N)

All four produce milliseconds since the Unix epoch.

Mistakes with millisecond timestamps

  • Using perf_counter() as a timestamp. It is not epoch time. Storing its values in a database or sending them to an API produces meaningless numbers.
  • Worrying about float precision with time.time(). Python floats (64-bit doubles) have about 15 significant digits. A Unix timestamp in 2026 is roughly 1.718 x 10^9 seconds, or 1.718 x 10^12 in milliseconds, still well within that precision. You will not see loss until roughly the year 2255.
  • Mixing up seconds, milliseconds and microseconds. time.time() returns seconds. Many APIs expect milliseconds, others microseconds. Check the API documentation.
  • Assuming datetime.now() is UTC. Without a timezone argument it returns local time. Use datetime.now(timezone.utc) for UTC.
  • Ignoring platform differences. On Windows, time.time() historically had poor resolution (15.6 ms). Python 3.11+ uses a higher-resolution clock on Windows, but if you need sub-millisecond precision, test on your target platform.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms