Python Components of Date
Parts of date in python
Date components refer to the individual parts that make up a date, such as the year, month, day, hour, minute, second,
and microsecond. In Python, you can easily extract these components from a datetime
object.
Here's how you can access these components:
Extracting Date Components
You can access the components of a datetime
object (like year, month, day, etc.) using their respective attributes.
import datetime
# Create a datetime object
dt = datetime.datetime(2025, 2, 9, 14, 30, 45)
# Extract individual components
year = dt.year
month = dt.month
day = dt.day
hour = dt.hour
minute = dt.minute
second = dt.second
microsecond = dt.microsecond
weekday = dt.weekday() # 0=Monday, 6=Sunday
print(f"Year: {year}")
print(f"Month: {month}")
print(f"Day: {day}")
print(f"Hour: {hour}")
print(f"Minute: {minute}")
print(f"Second: {second}")
print(f"Microsecond: {microsecond}")
print(f"Weekday: {weekday} (0=Monday, 6=Sunday)")
Output:
Year: 2025
Month: 2
Day: 9
Hour: 14
Minute: 30
Second: 45
Microsecond: 0
Weekday: 6 (0=Monday, 6=Sunday)
Accessing Date Components
If you are working with only the date (without time), you can access the components using the date
class.
import datetime
# Create a date object
date_obj = datetime.date(2025, 2, 9)
# Extract individual components
year = date_obj.year
month = date_obj.month
day = date_obj.day
weekday = date_obj.weekday() # 0=Monday, 6=Sunday
print(f"Year: {year}")
print(f"Month: {month}")
print(f"Day: {day}")
print(f"Weekday: {weekday} (0=Monday, 6=Sunday)")
Output:
Year: 2025
Month: 2
Day: 9
Weekday: 6 (0=Monday, 6=Sunday)
time
Object
Date Components in If you are working with just the time (without date), you can access the time components like hour, minute, second, and microsecond.
import datetime
# Create a time object
time_obj = datetime.time(14, 30, 45, 123456)
# Extract individual components
hour = time_obj.hour
minute = time_obj.minute
second = time_obj.second
microsecond = time_obj.microsecond
print(f"Hour: {hour}")
print(f"Minute: {minute}")
print(f"Second: {second}")
print(f"Microsecond: {microsecond}")
Output:
Hour: 14
Minute: 30
Second: 45
Microsecond: 123456
Weekday and Week Number
-
Weekday: You can get the day of the week (0 = Monday, 6 = Sunday) using
.weekday()
. -
Week Number: The week number (1-52) can be found using
.isocalendar()
.
import datetime
# Create a date object
date_obj = datetime.date(2025, 2, 9)
# Get the weekday (0=Monday, 6=Sunday)
weekday = date_obj.weekday()
print(f"Weekday: {weekday} (0=Monday, 6=Sunday)")
# Get the ISO calendar week number
iso_year, iso_week, iso_weekday = date_obj.isocalendar()
print(f"ISO Year: {iso_year}, ISO Week: {iso_week}, ISO Weekday: {iso_weekday}")
Output:
Weekday: 6 (0=Monday, 6=Sunday)
ISO Year: 2025, ISO Week: 6, ISO Weekday: 7
Working with Date Components in Pandas
If you're working with pandas
, it provides a simple way to access date components in Series
of dates.
import pandas as pd
# Create a pandas datetime series
dates = pd.to_datetime(['2025-02-09', '2025-03-01', '2025-05-15'])
# Extract date components
print(dates.dt.year) # Year
print(dates.dt.month) # Month
print(dates.dt.day) # Day
print(dates.dt.weekday) # Weekday (0=Monday, 6=Sunday)
print(dates.dt.isocalendar().week) # Week number
Output:
0 2025
1 2025
2 2025
dtype: int64
0 2
1 3
2 5
dtype: int64
0 9
1 1
2 15
dtype: int64
0 6
1 6
2 3
dtype: int64
0 6
1 9
2 19
dtype: int64
Date Components
- Year:
.year
- Month:
.month
- Day:
.day
- Hour:
.hour
- Minute:
.minute
- Second:
.second
- Microsecond:
.microsecond
- Weekday:
.weekday()
(0 = Monday, 6 = Sunday) - ISO Week Number:
.isocalendar().week