How do I get the current time in milliseconds in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The fastest way to get the current time in milliseconds in Python is int(time.time() * 1000). This gives you a Unix timestamp in milliseconds, which is the standard format used by JavaScript's Date.now(), Java's System.currentTimeMillis(), and most APIs that accept millisecond timestamps. Below are all the methods available, when to use each, and the differences that actually matter.
Method 1: time.time() (Recommended for Timestamps)
time.time() returns seconds since the Unix epoch (January 1, 1970 00:00:00 UTC) as a float. Multiplying by 1000 converts to milliseconds. Wrapping in int() truncates the fractional part.
When to use: Generating timestamps for logging, API calls, database records, or any situation where you need wall-clock time as an integer.
A Note on round() vs int()
Some guides suggest int(round(time.time() * 1000)). The round() call is unnecessary. The difference between truncation and rounding is at most 1 millisecond, and since time.time() itself has limited precision (platform-dependent, typically microseconds), the rounding does not improve accuracy.
Method 2: datetime.timestamp() (When You Already Have a datetime Object)
This produces the same result as time.time() but goes through a datetime object. It is useful when you already have a datetime instance and need to convert it to a millisecond timestamp:
When to use: Converting existing datetime objects to millisecond timestamps. Do not create a datetime object just to get the current time in milliseconds; use time.time() directly.
Method 3: time.time_ns() (Python 3.7+, Highest Precision)
time.time_ns() returns nanoseconds since the epoch as an integer, avoiding floating-point precision loss entirely. Dividing by 1,000,000 converts to milliseconds.
When to use: When you need to avoid any floating-point rounding artifacts, such as in financial systems or when comparing timestamps that are very close together.
Method 4: time.perf_counter() (For Measuring Elapsed Time)
time.perf_counter() provides the highest-resolution timer available on your platform for measuring short durations. It does not return wall-clock time or a Unix timestamp. The value has no defined relationship to any calendar epoch.
When to use: Benchmarking, profiling, measuring how long an operation takes. Never use this for timestamps.
Method 5: time.monotonic() (For Timeouts and Scheduling)
time.monotonic() returns a clock that never goes backward, even if the system clock is adjusted (NTP sync, manual change, DST). Like perf_counter(), it does not represent wall-clock time.
When to use: Implementing timeouts, retry delays, or any logic where you need a clock that is immune to system time changes.
Comparison Table
| Method | Returns | Epoch-Based | Precision | Use Case |
time.time() * 1000 | float | Yes | Microseconds (platform-dependent) | Timestamps, logging, APIs |
datetime.now().timestamp() * 1000 | float | Yes | Microseconds | Converting datetime objects |
time.time_ns() // 1_000_000 | int | Yes | Nanoseconds | High-precision timestamps |
time.perf_counter() * 1000 | float | No | Sub-microsecond | Benchmarking, profiling |
time.monotonic() * 1000 | float | No | Sub-microsecond | Timeouts, scheduling |
Timezone Considerations
time.time() always returns UTC. If you need milliseconds since epoch for a specific timezone, convert carefully:
Epoch timestamps are timezone-independent. The number of milliseconds since 1970-01-01T00:00:00Z is the same regardless of your local timezone. Timezones only matter when you format the timestamp as a human-readable string.
Cross-Language Equivalents
If you are working across languages, here are the equivalent calls:
All four produce the same value: milliseconds since the Unix epoch.
Common Pitfalls
- Using
perf_counter()as a timestamp. It does not return epoch time. Storingperf_counter()values in a database or sending them to an API will produce meaningless numbers. - Floating-point precision loss 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. Multiplied by 1000, that is 1.718 x 10^12, which is still well within 15-digit precision. You will not see precision loss until roughly the year 2255. For most applications,time.time() * 1000is fine. - Confusing seconds, milliseconds, and microseconds.
time.time()returns seconds. Many APIs expect milliseconds. Others expect microseconds. Always check the API documentation. - Assuming
datetime.now()is UTC. Without a timezone argument,datetime.now()returns local time. Usedatetime.now(timezone.utc)if you need UTC. - 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.
Summary
int(time.time() * 1000)is the simplest and most widely used approach for getting the current time in milliseconds.- Use
time.time_ns() // 1_000_000on Python 3.7+ if you need to avoid floating-point precision loss. - Use
time.perf_counter()for measuring elapsed time, not for generating timestamps. - Use
time.monotonic()for timeouts and scheduling that must be immune to system clock changes. - Epoch millisecond timestamps are timezone-independent. Timezone only affects display formatting.
Related reading
- How do I get the current time in milliseconds in Python?
- How do I get the current time in Python?
- How do I get the different parts of a Flask request's url?
- How do I get the filename without the extension from a path in Python?
- How do I get the id after INSERT into MySQL database with Python?
- How do I get the id after INSERT into MySQL database with Python?
- How do I get the last element of a list?
- How do I get the number of elements in a list length of a list in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.