Python
Programming
Time Measurement
Milliseconds
Coding Tips

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

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.

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

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

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:

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)

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)

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, 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)

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")

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)

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

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

Timezone Considerations

time.time() always returns UTC. If you need milliseconds since epoch for a specific timezone, convert carefully:

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

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:

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 the same value: milliseconds since the Unix epoch.

Common Pitfalls

  • Using perf_counter() as a timestamp. It does not return epoch time. Storing perf_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() * 1000 is 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. Use datetime.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_000 on 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
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.