How to Get the Current Time in Python

What you’ll build or solve

You’ll get the current time in Python and format it for logs, user-facing text, and filenames.

When this approach works best

This approach works best when you:

  • Print the current time in a CLI tool, like “Started at 14:03”.
  • Add timestamps to logs, like 2026-02-17 14:03:12.
  • Measure how long a task takes by recording a start time and end time.

Avoid using local time when your code runs on servers in multiple regions or when you store times for later comparisons. In those cases, use timezone-aware datetimes, usually in UTC.

Prerequisites

  • Python installed
  • You know what a variable is

Step-by-step instructions

1) Get the current local time

Use datetime.now() to get the current local date and time.

fromdatetimeimportdatetime

now=datetime.now()
print(now)

What to look for:

now is a datetime object. It includes both the date and the time, down to microseconds.


2) Extract and format just the time

If you only want the time part, call .time(). For a clean string, use strftime().

fromdatetimeimportdatetime

now=datetime.now()

current_time=now.time()
print(current_time)

print(now.strftime("%H:%M"))# 24-hour time, like 14:03
print(now.strftime("%I:%M %p"))# 12-hour time, like 02:03 PM

What to look for:

%H is 24-hour hours, %I is 12-hour hours, %M is minutes, %S is seconds.


3) Use a specific timezone (or UTC)

Local time depends on the machine running your code. If timezone matters, use zoneinfo to pick one, or use UTC.

fromdatetimeimportdatetime,timezone
fromzoneinfoimportZoneInfo

now_utc=datetime.now(timezone.utc)
print(now_utc)

now_in_podgorica=datetime.now(ZoneInfo("Europe/Podgorica"))
print(now_in_podgorica)
print(now_in_podgorica.strftime("%H:%M:%S"))

What to look for:

Timezone-aware datetimes have a timezone attached. They are safer for storing, comparing, and sharing across systems.


Examples you can copy

Example 1: Print the current time as HH:MM:SS

fromdatetimeimportdatetime

stamp=datetime.now().strftime("%H:%M:%S")
print(stamp)

Example 2: Add a timestamp to a log line

fromdatetimeimportdatetime

ts=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{ts}] Job started")

Example 3: Make a filename-friendly time stamp

fromdatetimeimportdatetime

ts=datetime.now().strftime("%Y%m%d-%H%M%S")
filename=f"backup-{ts}.zip"
print(filename)

Example 4: Measure how long something takes

Use perf_counter() for timing code. It is better than comparing wall-clock time.

fromtimeimportperf_counter

start=perf_counter()

total=0
foriinrange(1_000_000):
total+=i

end=perf_counter()
print("seconds:",end-start)

Example 5: Show the current time in a specific timezone

fromdatetimeimportdatetime
fromzoneinfoimportZoneInfo

ny=datetime.now(ZoneInfo("America/New_York")).strftime("%H:%M")
tokyo=datetime.now(ZoneInfo("Asia/Tokyo")).strftime("%H:%M")

print("New York:",ny)
print("Tokyo:",tokyo)

Common mistakes and how to fix them

Mistake 1: Using naive datetimes for cross-timezone work

What you might do

fromdatetimeimportdatetime

now=datetime.now()
print(now)

Why it breaks

now has no timezone info attached. If you store it and read it on a different machine, “what time was this?” becomes unclear.

Fix

Use UTC or a named timezone.

fromdatetimeimportdatetime,timezone

now_utc=datetime.now(timezone.utc)
print(now_utc)

Mistake 2: Trying to parse time strings with strftime()

What you might do

fromdatetimeimportdatetime

t=datetime.strftime("14:03","%H:%M")

Why it breaks

strftime() formats a datetime into a string. It does not parse strings.

Fix

Use strptime() to parse, then get the time part.

fromdatetimeimportdatetime

dt=datetime.strptime("14:03","%H:%M")
print(dt.time())

Troubleshooting

If you see ImportError: cannot import name 'ZoneInfo', upgrade to Python 3.9+ or use UTC with datetime.now(timezone.utc).

If your time looks “off” on a server, the server's timezone may be different than your local machine. Use a specific timezone with ZoneInfo(...).

If you need accurate timing for performance, use time.perf_counter() instead of datetime.now().

If formatting gives unexpected output, print the raw datetime.now() first, then adjust your strftime() format codes.


Quick recap

  • Use datetime.now() to get the current local date and time.
  • Use .time() to extract the time, or strftime() to format it.
  • Use timezone.utc or ZoneInfo("Region/City") when timezone matters.
  • Use perf_counter() to measure elapsed time accurately.