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
Getting the current time in milliseconds in Python is easy once you separate two different needs: wall-clock timestamps and elapsed-time measurement. If you want “what time is it right now” as milliseconds since the Unix epoch, use an epoch-based API; if you want to measure how long something took, use a monotonic performance clock instead.
Unix Timestamp in Milliseconds
If the requirement is the current timestamp, the usual answer is time.time() or, more precisely on modern Python, time.time_ns().
That gives the number of milliseconds since January 1, 1970 UTC. It works well in many scripts, but the value starts as a float, so very fine precision is rounded before you convert it to an integer.
A better modern approach is to skip floating-point conversion entirely.
If your Python version supports time_ns, this is usually the cleanest direct answer.
datetime Is Useful When You Already Need Calendar Data
If your code already works with datetime, you can still produce milliseconds from a timestamp-aware datetime object.
This is not as direct as time.time_ns(), but it is convenient when the same value also needs formatting, timezone conversion, or storage as a calendar timestamp.
Measuring Durations Requires a Different Clock
Wall-clock time is not the right tool for measuring elapsed runtime because the system clock can jump due to synchronization or manual changes. For duration measurement, use a monotonic or performance counter.
perf_counter_ns() is designed for benchmarks and short-duration timing. It does not tell you the date or time of day, but it is much better for “how many milliseconds passed” questions.
Rounding, Truncation, and Storage
When converting to milliseconds, decide whether truncation or rounding matters. Integer division such as // 1_000_000 truncates toward zero, which is usually correct for timestamps stored as integer milliseconds.
For most application code, truncation is acceptable and easier to reason about. More important than the rounding rule is storing the result as an integer, not as a float.
Pick the API by Intent
A practical rule set is:
- use
time.time_ns() // 1_000_000for current epoch milliseconds - use
datetime.now(timezone.utc)when you also need a real calendar object - use
time.perf_counter_ns()for benchmarks and elapsed duration - use
time.monotonic_ns()for timeout logic that must ignore wall-clock changes
The exact same phrase “time in milliseconds” can refer to any of these, so clarity about intent matters more than memorizing one function name.
Common Pitfalls
- Using
time.time()for performance timing when a monotonic clock is the correct tool. - Creating naive
datetimevalues when timezone-aware timestamps are needed. - Treating epoch milliseconds and elapsed milliseconds as if they were the same concept.
- Keeping the result as a float instead of converting it to an integer for storage or transport.
- Ignoring
time.time_ns()even though it avoids precision loss from float multiplication.
Summary
- Use
time.time_ns() // 1_000_000for the current Unix timestamp in milliseconds. - '
datetimeis helpful when you also need timezone-aware calendar handling.' - Use
perf_counter_ns()ormonotonic_ns()for elapsed timing rather than wall-clock timestamps. - Store millisecond values as integers whenever possible.
- Choose the clock based on whether you need an absolute timestamp or a duration measurement.
Related reading
- 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?
- How do I get the object if it exists, or None if it does not exist in Django?
.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.