🕯️ Magic Note
The datetime module provides classes for manipulating dates and times. datetime.now() returns a datetime object representing the current date and time according to the system clock. This object has several attributes, including .year, .month, .day, .hour, .minute, and .second. Accessing .year gives you the four digit year (e.g., 2025, 2026) as an integer.
- Requires from datetime import datetime or import datetime
- .year returns an integer, not a string
- The year is always four digits (e.g., 2025, 2026)
- Uses your system’s local time; for UTC use datetime.now(timezone.utc)
| Expression | Example Output (in 2026) | Type |
|---|---|---|
| datetime.now().year | 2026 | int |
| datetime.now().month | 1 | int |
| datetime.now().day | 1 | int |
| datetime.now().year + 1 | 2027 | int |
| str(datetime.now().year) | “2026” | str |
Python
# Getting the current year
from datetime import datetime
current_year = datetime.now().year
print(current_year)
# Output: 2026 (or the actual current year)
print(type(current_year))
# Output: <class ‘int’>
Python
# Using the year in a copyright notice
from datetime import datetime
start_year = 2022
current_year = datetime.now().year
if current_year > start_year:
copyright_text = f”© {start_year}–{current_year} Feloriya”
else:
copyright_text = f”© {start_year} Feloriya”
print(copyright_text)
# Output: © 2022–2026 Feloriya (or current year)
Python
# Full date and time extraction
from datetime import datetime
now = datetime.now()
print(f”Year: {now.year}”)
print(f”Month: {now.month}”)
print(f”Day: {now.day}”)
print(f”Hour: {now.hour}”)
print(f”Minute: {now.minute}”)
# Output: Year: 2026, Month: 1, Day: 1, … (based on current time)
- Forgetting to import datetime, causing a NameError
- Calling datetime.now.year without parentheses, missing the function call now()
- Assuming .year returns a string when it returns an integer, causing issues in concatenation
⚡ Whisper
The year did not wait for you to be ready. It moved forward. So should you. Python reaches into the invisible river of time and pulls out the year. A number. A reminder. Better things are ahead. Time will bring them. You just need to keep going.