36 entries

Current date & time
Get the current date, time, or datetime
Create specific date
Construct a date or datetime from components
From timestamp
Convert Unix timestamp to datetime and back
From ISO format string
Parse ISO 8601 format strings
strftime - format to string
Convert datetime to a formatted string
strptime - parse from string
Parse a string into a datetime object
Format codes cheat sheet
All strftime/strptime format codes
ISO format output
Convert to ISO 8601 string
Create timedelta
Represent a duration (difference between two dates/times)
Date arithmetic
Add/subtract time from dates and datetimes
Timedelta to human-readable
Extract days, hours, minutes from a timedelta
UTC datetime
Create timezone-aware datetime in UTC
zoneinfo - named timezones
Use IANA timezone names (Python 3.9+)
Convert between timezones
Convert a datetime from one timezone to another
List available timezones
Get all valid timezone names
Compare dates
Check if a date is before, after, or equal to another
Extract components
Get year, month, day, hour, minute, etc.
Day of week, week number
Get weekday name and ISO week number
Replace components
Create a new datetime with some fields changed
f-string number formatting
Format numbers with commas, decimals, padding
Percentage & scientific
Format as percentage, scientific notation, or binary/hex
format() and locale
Format numbers with locale-specific separators
Currency formatting
Format money values properly
Basic math functions
abs, round, min, max, pow, divmod
math module essentials
sqrt, log, ceil, floor, pi, e, inf
Trigonometry & radians
sin, cos, tan, degrees/radians conversion
Combinatorics
factorial, comb, perm, gcd, lcm
Float precision gotchas
Why 0.1 + 0.2 != 0.3 and how to handle it
Decimal for precision
Exact decimal arithmetic for money and science
int vs float conversion
Converting between numeric types safely
Age from birthday
Calculate someone's age from their date of birth
Business days / weekdays
Count or iterate over weekdays between two dates
Start/end of month
Get the first and last day of any month
Date range generator
Generate a sequence of dates between two dates
Measure execution time
Time how long code takes to run
Human-readable time ago
Convert a datetime to "2 hours ago" format

Frequently Asked Questions

What is the difference between strftime and strptime?
strftime (string format time) converts a datetime object to a formatted string: dt.strftime("%Y-%m-%d") produces "2026-03-25". strptime (string parse time) does the reverse and parses a string into a datetime: datetime.strptime("2026-03-25", "%Y-%m-%d"). Remember: f = format (to string), p = parse (from string).
Why does 0.1 + 0.2 not equal 0.3 in Python?
Floating-point numbers use binary (base-2) representation, and 0.1 cannot be exactly represented in binary, similar to how 1/3 cannot be exactly written in decimal. The result 0.30000000000000004 is the closest representable binary fraction. Use math.isclose() for comparisons, Decimal for exact arithmetic (especially money), or round() for display.
Should I use datetime.now() or datetime.utcnow()?
Neither! datetime.utcnow() is deprecated in Python 3.12+. Use datetime.now(timezone.utc) for timezone-aware UTC. For local time with timezone info, use datetime.now(ZoneInfo("America/New_York")). Always store and compute in UTC, convert to local time only for display.
How do I handle timezones in Python?
Use the zoneinfo module (Python 3.9+) with IANA timezone names. Create timezone-aware datetimes: datetime(2026, 1, 1, tzinfo=ZoneInfo("US/Eastern")). Convert with .astimezone(). Never mix naive (no timezone) and aware datetimes because it raises a TypeError.
What is the difference between round(), math.floor(), and math.ceil()?
round() uses banker's rounding (round-half-to-even): round(0.5)=0, round(1.5)=2. math.floor() always rounds down: floor(3.9)=3, floor(-3.1)=-4. math.ceil() always rounds up: ceil(3.1)=4, ceil(-3.9)=-3. int() truncates toward zero: int(3.9)=3, int(-3.9)=-3.

About This Reference

This reference covers everything you need for working with numbers, dates, and times in Python. From creating and formatting datetimes with strftime/strptime, to timezone handling with zoneinfo, to f-string number formatting and the Decimal module for precise arithmetic.

Every example is copy-paste ready. Complements the Strftime Playground and Unix Timestamp Converter tools.