Date & Time#
Python handles dates and times through the time and datetime modules.
Module time#
The time module provides functions for working with time values represented as seconds since the epoch (January 1, 1970).
Time Structure#
time.localtime() returns a named tuple with 9 attributes:
Index |
Attribute |
Description |
Values |
|---|---|---|---|
0 |
tm_year |
4-digit year |
e.g. 2025 |
1 |
tm_mon |
Month |
1–12 |
2 |
tm_mday |
Day |
1–31 |
3 |
tm_hour |
Hour |
0–23 |
4 |
tm_min |
Minute |
0–59 |
5 |
tm_sec |
Second |
0–59 |
6 |
tm_wday |
Day of week |
0–6 (Monday=0) |
7 |
tm_yday |
Day of year |
1–366 |
8 |
tm_isdst |
Daylight saving |
-1, 0, 1 |
Format Directives#
Directive |
Meaning |
|---|---|
|
Abbreviated / full weekday name |
|
Abbreviated / full month name |
|
Day of month (01–31) |
|
Hour: 24h (00–23) / 12h (01–12) |
|
Day of year (001–366) |
|
Month (01–12) |
|
Minute (00–59) |
|
Second (00–59) |
|
Year without / with century |
|
Time zone name |
Main Functions#
Function |
Description |
|---|---|
|
Current local time as a named tuple |
|
Current time as seconds since epoch (float) |
|
Format a time tuple as a string |
|
Pause execution for the given number of seconds |
>>> import time
>>> time.localtime()
time.struct_time(tm_year=2025, tm_mon=3, tm_mday=4, ...)
>>> time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
'Tue, 04 Mar 2025 11:56:30'
Measuring Elapsed Time#
import time
print(time.strftime("%H:%M:%S"))
start = time.time()
# Simulate a process
time.sleep(3.7)
elapsed = time.time() - start
hours = int(elapsed // 3600)
minutes = int((elapsed % 3600) // 60)
seconds = int(elapsed % 60)
print(f"Time: {time.strftime('%H:%M:%S')}")
print(f"Processing time: {hours:02d}:{minutes:02d}:{seconds:02d}")
Module datetime#
The datetime module provides classes for manipulating dates and times.
Main Classes#
Class |
Description |
|---|---|
|
A date (no time component) |
|
A time (no date component) |
|
Combined date and time |
|
A duration between two dates/times |
Main Methods#
Method |
Description |
|---|---|
|
Current local date |
|
Current local date and time |
|
Return a copy with specified fields replaced |
|
ISO 8601 string (YYYY-MM-DDTHH:MM:SS) |
|
Access individual components |
|
Day of week (Monday=0, Sunday=6) |
Examples#
>>> import datetime
>>> today = datetime.date(2025, 8, 6)
>>> one_day = datetime.timedelta(days=1)
>>> today + one_day
datetime.date(2025, 8, 7)
>>> birth = datetime.datetime(1974, 2, 7, 20, 45, 50)
>>> now = datetime.datetime(2025, 3, 16, 10, 38, 2)
>>> diff = now - birth
>>> diff.days
18665
>>> diff.seconds
49932
>>> years = diff.days // 365
>>> remaining_days = diff.days % 365
>>> hours = diff.seconds // 3600
>>> minutes = (diff.seconds % 3600) // 60
>>> print(f"{years} years, {remaining_days} days, {hours}h {minutes}min")
51 years, 40 days, 13h 52min