Python
Current Time
Milliseconds
datetime
Programming

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.

Browse interview questions

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().

python
1import time
2
3milliseconds = int(time.time() * 1000)
4print(milliseconds)

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.

python
1import time
2
3milliseconds = time.time_ns() // 1_000_000
4print(milliseconds)

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.

python
1from datetime import datetime, timezone
2
3now = datetime.now(timezone.utc)
4milliseconds = int(now.timestamp() * 1000)
5print(milliseconds)

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.

python
1import time
2
3start = time.perf_counter_ns()
4
5sum(range(1_000_000))
6
7elapsed_ms = (time.perf_counter_ns() - start) / 1_000_000
8print(elapsed_ms)

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.

python
1import time
2
3ms_floor = time.time_ns() // 1_000_000
4ms_round = round(time.time_ns() / 1_000_000)
5print(ms_floor, ms_round)

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_000 for 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 datetime values 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_000 for the current Unix timestamp in milliseconds.
  • 'datetime is helpful when you also need timezone-aware calendar handling.'
  • Use perf_counter_ns() or monotonic_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
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
Track 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.

Browse interview questions

All Rights Reserved.