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
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
This gives the same result as time.time() via a datetime object. It is useful for converting an existing datetime:
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+)
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
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
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.
| 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 |
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:
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
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. Usedatetime.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.
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 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?
- How do I get the opposite negation of a Boolean in Python?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo 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 courseData 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.