How to get the current date and time in Python
Import datetime and call datetime.now(): from datetime import datetime, then now = datetime.now(). That gives you a datetime object holding the date and the time — call now.strftime("%H:%M:%S") when you want it as text.
Two things trip people up here. First, datetime.now() returns an object, not a string — printing it looks like text, but you format it with strftime() before putting it anywhere real. Second, that object is naive: it carries no time zone, just whatever the clock on the machine says. The snippets below run on our server, whose clock is set to UTC — so "now" on this page is UTC time, which is exactly the surprise you'd hit on a production box. Hit Run, then edit the code and run it again.
1datetime.now()Recommended
datetime.now() reads the system clock and hands back a datetime — date and time in one object, down to the microsecond. Every part of it is available as an attribute or as a smaller object:
Output
Your run prints the moment you pressed Run, in the shape YYYY-MM-DD HH:MM:SS.ffffff. .date() and .time() split it apart; if you only ever want the date, date.today() skips the time entirely.
2Format it as a string with strftime()
strftime() ("string format time") turns a datetime into text using % codes: %Y four-digit year, %m month, %d day, %H hour on a 24-hour clock, %M minute, %S second. The second line formats a fixed date so you can see exactly what each code produces:
Output
The second line always prints 23 Jul 2026 at 02:05 PM — %b is the abbreviated month, %I the 12-hour clock and %p the AM/PM marker. For a machine-readable string, skip the codes and use now.isoformat().
3UTC and other time zones
A naive datetime is a bug waiting to happen the moment two machines compare notes. Pass a time zone to now() and you get an aware datetime that knows its offset: timezone.utc for UTC, and ZoneInfo (standard library since Python 3.9) for a real region with daylight-saving rules baked in:
Output
Note the offsets in the output — +00:00 for UTC, -04:00 or -05:00 for New York depending on daylight saving. Don't reach for datetime.utcnow(): it's deprecated since Python 3.12 ("scheduled for removal in a future version") precisely because it returned a naive object that only looked like UTC.
4The current Unix timestamp
For logs, APIs and anything you need to store or subtract, a Unix timestamp — seconds since 1 January 1970 UTC — beats a formatted string. time.time() gives you one as a float, and datetime.fromtimestamp() converts back:
Output
The round trip is exact: 1700000000 is 2023-11-14 22:13:20+00:00, and that datetime's .timestamp() is 1700000000.0 again. Always pass timezone.utc to fromtimestamp() — without it you get the server's local reading of that instant. Milliseconds are just int(time.time() * 1000).
5Which should you use?
| Method | Returns | Time zone | Best for |
|---|---|---|---|
| datetime.now() | datetime object | Naive (machine local) | Showing the time to one local user |
| datetime.now(timezone.utc) | Aware datetime | UTC | Storing and comparing across machines |
| date.today() | date object | Naive (machine local) | Today's date, no time needed |
| time.time() | float seconds | UTC epoch | Logs, APIs, arithmetic |
| time.perf_counter() | float seconds | None (monotonic) | Measuring how long code takes |
6Common variation: how long did that take?
Reading the clock twice and subtracting is the usual next question — and datetime.now() is the wrong tool for it. Use time.perf_counter(), a high-resolution monotonic clock that only ever moves forward:
Output
Prints took 0.5s. perf_counter()'s value is meaningless on its own — only differences matter — but unlike a wall clock it can't jump backwards when the system clock is corrected or daylight saving flips, so your durations never come out negative.
Frequently asked questions
Why does datetime.now() show the wrong time?
It isn't wrong — it's the clock and time zone of the machine running the code, with no time-zone information attached. Servers are usually set to UTC (the sandbox on this page is), so now() there is UTC rather than your local time. Use datetime.now(timezone.utc) when you want UTC explicitly, or datetime.now(ZoneInfo("America/New_York")) for a specific region.
How do I get just the current date, or just the time?
Call .date() or .time() on the result: datetime.now().date() gives a date, datetime.now().time() gives a time. If you never need the time part, from datetime import date and date.today() is the direct route.
Is datetime.utcnow() deprecated?
Yes — since Python 3.12 it raises a DeprecationWarning saying it is scheduled for removal in a future version. The problem was that it returned a *naive* datetime holding UTC values, which silently compared wrong against aware datetimes. Use datetime.now(timezone.utc) instead.
How do I get the current time in a specific time zone?
Pass a ZoneInfo to now(): datetime.now(ZoneInfo("Asia/Tokyo")). zoneinfo is in the standard library from Python 3.9 and uses IANA names like Europe/London or America/Sao_Paulo. On Windows the time-zone database may be missing — pip install tzdata fixes it.
How do I get the current time in milliseconds?
Multiply the Unix timestamp: int(time.time() * 1000) gives milliseconds since the epoch. From a datetime, now.microsecond is the sub-second part, so now.microsecond // 1000 is the millisecond within that second.
How do I show the time in 12-hour format with AM/PM?
Use %I for the 12-hour clock and %p for the marker: datetime.now().strftime("%I:%M %p") prints something like 02:05 PM. %H is the 24-hour equivalent, and %-I (Linux/macOS) drops the leading zero.