PythonMastery

How do I get the current date and time in Python?

datetime.now() for local time, date.today() for just the date, datetime.now(timezone.utc) for anything you store or send. Then format it with strftime.

python
from datetime import date, datetime

print(datetime.now())
print(date.today())
example output · yours will differ
2026-10-07 14:32:05.118204
2026-10-07

Formatting it for people

strftime turns a datetime into whatever text you need. The same codes work inside an f-string after a colon:

python
from datetime import datetime

moment = datetime(2026, 10, 7, 14, 32, 5)   # a fixed time, so the output below is exact

print(moment.strftime("%d/%m/%Y %H:%M"))
print(moment.strftime("%A %d %B %Y"))
print(f"{moment:%Y-%m-%d}")
print(moment.isoformat())
output
07/10/2026 14:32
Wednesday 07 October 2026
2026-10-07
2026-10-07T14:32:05

The codes you'll use most: %Y year, %m month number, %d day, %H hour (24-hour), %M minutes, %S seconds, %A weekday name, %B month name. Swap %H for %I and add %p for 12-hour time with AM/PM.

Store UTC, show local

datetime.now() returns the time on this computer, with no time zone attached. Save that to a database from a server in London and read it on a laptop in Mumbai, and you're 4½ or 5½ hours off, depending on the season, without any error to warn you.

For anything you store, log or send to another system, take the time in UTC and keep the zone on it:

python
from datetime import datetime, timezone

now_utc = datetime.now(timezone.utc)
print(now_utc.isoformat())
print(now_utc.tzinfo)
example output · yours will differ
2026-10-07T09:02:05.118204+00:00
UTC

The +00:00 at the end travels with the value, so any program reading it knows exactly which moment it means. Convert to someone's local time only when you show it to them.

Just the time, or a timestamp

python
from datetime import datetime
import time

print(datetime.now().strftime("%H:%M:%S"))
print(int(time.time()))     # seconds since 1 January 1970 UTC, the "Unix timestamp"
example output · yours will differ
14:32:05
1791363725

To measure how long something takes, don't subtract two datetime.now() values; use time.perf_counter(), which is made for it. How to time your code shows why.

Go deeper: Dates, Times, and Timezones, Python Datetime Format Cheat Sheet

More short answers

all questions