How do I get the current time 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 Python is easy, but the right function depends on what you actually need. Sometimes you want a local wall-clock time for display, sometimes a timezone-aware UTC timestamp for storage, and sometimes just epoch seconds for measurement.
The biggest mistake is treating all of those as the same thing. Python gives you several time APIs because they solve different problems.
Use datetime.now() for a Local Date and Time
If you want the current local date and time, the most direct option is datetime.now().
This returns a datetime object representing the current local time according to the machine running the code. It is convenient for quick scripts, logs, and human-readable output.
You can format it with strftime:
Prefer Timezone-Aware UTC for Storage
For many applications, UTC is a better default than local time. It is unambiguous and easier to compare across systems.
This returns a timezone-aware datetime. That is usually preferable to a naive local datetime when writing timestamps to databases, APIs, or logs that may be read in multiple time zones.
If You Only Need the Time Portion
Sometimes you want just the current time of day.
This gives you a time object, which is useful for display or scheduling logic that does not need the full date.
Use time.time() for Epoch Seconds
If you need a Unix timestamp, use time.time().
This returns the number of seconds since the Unix epoch as a floating-point value. It is useful for simple timing, interoperability, or compact numeric storage.
Do not confuse this with a formatted human time. It is a machine-oriented representation.
Measuring Elapsed Time Is a Different Task
If your goal is performance measurement, time.time() is not always the best tool. Python provides timers intended for elapsed time measurement.
This is better for benchmarking because it is designed for measuring durations, not just reporting the current wall-clock time.
Converting to Another Time Zone
Once you have a timezone-aware datetime, converting it is straightforward.
Using zoneinfo from the standard library is the modern built-in way to work with time zones in current Python versions.
Which One Should You Choose?
A useful rule is:
- use
datetime.now()for local human-facing time - use
datetime.now(timezone.utc)for stored or transmitted timestamps - use
time.time()for epoch seconds - use
time.perf_counter()for measuring elapsed time
When you choose the function based on the job, the code becomes clearer and fewer timezone bugs slip in later.
Common Pitfalls
- Using local naive datetimes for database or API timestamps that really should be UTC.
- Treating
time.time()as if it were a readable local time string. - Using wall-clock time APIs to measure code performance instead of
perf_counter(). - Forgetting that a
datetimewithout timezone information can become ambiguous when shared across systems. - Formatting time too early and then losing the ability to do date arithmetic cleanly.
Summary
- '
datetime.now()gives you the current local date and time.' - '
datetime.now(timezone.utc)is usually the safer choice for storage and APIs.' - '
time.time()returns epoch seconds, not a human-friendly formatted timestamp.' - '
time.perf_counter()is the right tool for measuring elapsed time.' - Pick the time API based on whether you need display, storage, interoperability, or benchmarking.
Related reading
- 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?
- How do I get the opposite negation of a Boolean 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.