Python datetime
Generate Python date/time code for the standard library, dateutil, and arrow. Pick a module and operation — get copy-ready code instantly.
-- Select a module and operation above
How to Use This Tool
Select a Python module from the tabs — the standard library datetime, the popular dateutil extension, or the human-friendly arrow library. Then choose an operation from the chip selector. The tool generates practical, copy-ready Python code with real-world examples and comments explaining each step.
This is especially useful when you can never remember whether it's strftime or strptime, what format code gives you a zero-padded month, or how to properly handle timezones. Instead of digging through docs, pick your operation and copy the code.
strftime / strptime Format Codes
The complete reference for Python's date formatting and parsing. These codes work with both strftime() (format to string) and strptime() (parse from string).
| Code | Meaning | Example |
|---|---|---|
| %Y | 4-digit year | 2026 |
| %y | 2-digit year | 26 |
| %m | Month (zero-padded) | 03 |
| %B | Full month name | March |
| %b | Abbreviated month | Mar |
| %d | Day of month (zero-padded) | 26 |
| %j | Day of year (001–366) | 085 |
| %H | Hour, 24-hour (zero-padded) | 14 |
| %I | Hour, 12-hour (zero-padded) | 02 |
| %M | Minute (zero-padded) | 30 |
| %S | Second (zero-padded) | 45 |
| %f | Microsecond (000000–999999) | 123456 |
| %p | AM / PM | PM |
| %A | Full weekday name | Thursday |
| %a | Abbreviated weekday | Thu |
| %w | Weekday number (0=Sun, 6=Sat) | 4 |
| %u | ISO weekday (1=Mon, 7=Sun) | 4 |
| %U | Week number (Sun start) | 12 |
| %W | Week number (Mon start) | 12 |
| %V | ISO week number | 13 |
| %z | UTC offset (+HHMM) | -0500 |
| %Z | Timezone name | EST |
| %c | Locale date+time | Thu Mar 26 14:30:45 2026 |
| %x | Locale date | 03/26/26 |
| %X | Locale time | 14:30:45 |
| %% | Literal % | % |
Common Format Patterns
| Pattern | strftime Code | Output |
|---|---|---|
| ISO 8601 | %Y-%m-%dT%H:%M:%S | 2026-03-26T14:30:45 |
| US date | %m/%d/%Y | 03/26/2026 |
| EU date | %d/%m/%Y | 26/03/2026 |
| Long date | %B %d, %Y | March 26, 2026 |
| Short date | %b %d, %Y | Mar 26, 2026 |
| 24-hour time | %H:%M:%S | 14:30:45 |
| 12-hour time | %I:%M %p | 02:30 PM |
| Full timestamp | %Y-%m-%d %H:%M:%S | 2026-03-26 14:30:45 |
| Log format | %d/%b/%Y:%H:%M:%S %z | 26/Mar/2026:14:30:45 -0500 |
Module Comparison
When to use each library, and what it costs.
| Feature | datetime (stdlib) | dateutil | arrow |
|---|---|---|---|
| Install | Built-in | pip install python-dateutil | pip install arrow |
| Create dates | datetime(), date() | parse() — flexible string parsing | arrow.now(), arrow.get() |
| Add months/years | No (only timedelta: days/seconds) | relativedelta(months=3) | shift(months=3) |
| Timezone handling | zoneinfo (3.9+) or pytz | tz.gettz(), tz.UTC | Built-in .to('US/Eastern') |
| Flexible parsing | strptime (strict format required) | parser.parse() (auto-detect format) | arrow.get() (auto or format) |
| Human-readable | Manual formatting | Manual formatting | .humanize() → "2 hours ago" |
| Best for | Standard operations, no dependencies | Complex date math, flexible parsing | Human-friendly API, quick scripts |
Timezone Handling
Timezones are the most common source of datetime bugs in Python. Here's what you need to know.
Naive vs Aware datetimes
A naive datetime has no timezone info — dt.tzinfo is None. It's just a date and time floating in space with no anchor to a real moment. An aware datetime carries timezone information and unambiguously represents a single point in time. You cannot compare, subtract, or mix naive and aware datetimes — Python raises TypeError.
zoneinfo vs pytz
| Feature | zoneinfo (Python 3.9+) | pytz (legacy) |
|---|---|---|
| Import | from zoneinfo import ZoneInfo | import pytz |
| Create aware dt | datetime.now(ZoneInfo('US/Eastern')) | datetime.now(pytz.timezone('US/Eastern')) |
| Convert tz | dt.astimezone(ZoneInfo('Asia/Tokyo')) | dt.astimezone(pytz.timezone('Asia/Tokyo')) |
| UTC | from datetime import timezone; timezone.utc | pytz.utc |
| Install | Built-in (3.9+) | pip install pytz |
| Recommendation | Use this for new projects | Only for Python < 3.9 |
Common Patterns & Recipes
Parse dates from user input safely
Calculate age from birthdate
Find next weekday from today
Frequently Asked Questions
How do I get the current date and time in Python?
from datetime import datetime; now = datetime.now() gives you the local date and time. For UTC: datetime.now(timezone.utc). For just the date: from datetime import date; today = date.today(). These are all part of the standard library.
What is the difference between strftime and strptime?
strftime (string format time) converts a datetime to a string: dt.strftime('%Y-%m-%d') produces '2026-03-26'. strptime (string parse time) converts a string to a datetime: datetime.strptime('2026-03-26', '%Y-%m-%d'). The mnemonic: f = format (to string), p = parse (from string).
How do I add months to a date in Python?
The standard library's timedelta only supports days and seconds — not months or years, because months have variable lengths. Use dateutil.relativedelta: from dateutil.relativedelta import relativedelta; dt + relativedelta(months=3). Or with arrow: arrow.now().shift(months=3).
How do I handle timezones in Python?
Python 3.9+: use the built-in zoneinfo module. from zoneinfo import ZoneInfo; dt = datetime.now(ZoneInfo('America/New_York')). For older Python, use pytz. Always store datetimes as UTC internally and convert to local time only for display. Never use naive datetimes for anything that crosses timezone boundaries.
What is the difference between naive and aware datetimes?
A naive datetime has no timezone (tzinfo is None) — it's ambiguous. An aware datetime carries timezone information and represents an exact moment. Python won't let you compare or subtract naive and aware datetimes. Best practice: always create aware datetimes from the start using datetime.now(timezone.utc).
How do I convert a Unix timestamp to a datetime in Python?
datetime.fromtimestamp(ts) gives local time; datetime.fromtimestamp(ts, tz=timezone.utc) gives UTC. Going the other way: dt.timestamp() returns the Unix epoch as a float (Python 3.3+). For milliseconds, multiply or divide by 1000.