Dates & Times
Python - A Quick Start for existing Programers
3 min read
Published Sep 16 2025, updated Sep 30 2025
Guide Sections
Guide Comments
Python provides the datetime module for working with dates, times, and timestamps. This module has the following objects that you can import:
date- represents a calendar date. Use if you only care about year, month, day.time- represents a time of day. Use if you only care about hour, minute, second.datetime- represents a combined date and time. Use for everything together and arithmetic.timedelta- represents a duration or difference between dates/times. Use for durations and date/time calculations.
Import the objects required:
date object
Creation:
Attributes:
year,month,day
Methods:
today()→ current datefromtimestamp(ts)→ date from Unix timestampweekday()→ 0=Monday … 6=Sundayisoweekday()→ 1=Monday … 7=Sundayisoformat()→ 'YYYY-MM-DD'replace(year=…, month=…, day=…)→ return new date with replaced fieldstimetuple()→ struct_time tuple
Operations:
date1 - date2→timedelta- Comparisons:
<,>,==, etc.
time object
Creation:
Attributes:
hour,minute,second,microsecond,tzinfo
Methods:
isoformat()→ 'HH:MM:SS.microseconds'replace(hour=…, minute=…, second=…)→ new time with replaced fields
Note: time objects do not support arithmetic (use datetime + timedelta instead).
datetime object
Creation:
Attributes:
year, month, day, hour, minute, second, microsecond, tzinfo
Methods:
date()→ date parttime()→ time partreplace(...)→ replace componentsisoformat()→ 'YYYY-MM-DDTHH:MM:SS.microseconds'strftime(format)→ formatted stringstrptime(string, format)→ parse string into datetimetimestamp()→ seconds since epoch
Operations:
datetime ± timedelta→ new datetimedatetime1 - datetime2→ timedelta- Comparisons:
<,>,==, etc.
timedelta object
Creation:
Attributes:
days,seconds,microseconds
Methods:
total_seconds()→ total duration in seconds
Operations:
datetime + timedelta→ new datetimedatetime - timedelta→ new datetimetimedelta ± timedelta→ new timedelta- Comparisons:
<,>,==, etc.
Formatting output of datetime
The following methods use the codes below:
.strftime()to format a datetime into a string..strptime()with the same codes to parse a string back into datetime.isoformat()is a quick way for standardised output:2025-09-15T14:30:45.
Code | Meaning | Example |
| Year (4 digits) |
|
| Year (2 digits) |
|
| Month (01–12) |
|
| Full month name |
|
| Abbreviated month name |
|
| Day of month (01–31) |
|
| Day of year (001–366) |
|
| Week number of year (Sunday as first day, 00–53) |
|
| Week number of year (Monday as first day, 00–53) |
|
| ISO week number (01–53) |
|
| Full weekday name |
|
| Abbreviated weekday name |
|
| Weekday as number (0=Sunday, 6=Saturday) |
|
| Locale’s date representation |
|
| Hour (00–23) |
|
| Hour (01–12) |
|
| AM/PM |
|
| Minute (00–59) |
|
| Second (00–59) |
|
| Microsecond (000000–999999) |
|
| Locale’s time representation |
|
| Locale’s date and time |
|
| Locale’s date |
|
| Locale’s time |
|
| A literal |
|
Examples:
Demo of date and time objects
Converting between timezones
You can use the zoneinfo module to convert between timezones:
- Create the
datetime - Attach the
timezonethat it represents - Use the
astimezonein combination with theZoneInfomethod to convert to the new datetime of the new timezone
Example:














