from datetime import date, datetime print(datetime.now()) print(date.today())
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:
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())
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:
from datetime import datetime, timezone now_utc = datetime.now(timezone.utc) print(now_utc.isoformat()) print(now_utc.tzinfo)
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
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"
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.