In Python, dates
can be handled using the datetime
module, which provides classes for manipulating dates and times
.
Importing the datetime
Module
import datetime
Key Classes in datetime
- datetime.date: Represents a date (year, month, and day).
- datetime.time: Represents a time (hour, minute, second, microsecond).
- datetime.datetime: Combines date and time.
- datetime.timedelta: Represents the difference between two dates or times.
- datetime.tzinfo: Provides timezone information.
datetime.date
Class
The date
class is used to manipulate dates.
Creating a Date
from datetime import date # Create a date object d = date(2023, 7, 20) print(d) # Output: 2023-07-20
Getting Current Date
from datetime import date today = date.today() print(today) # Output: Current date, e.g., 2024-07-20
Getting Date Attributes
from datetime import date d = date(2023, 7, 20) print(d.year) # Output: 2023 print(d.month) # Output: 7 print(d.day) # Output: 20
datetime.time
Class
The time
class is used to manipulate times.
Creating a Time
from datetime import time # Create a time object t = time(14, 30, 45) print(t) # Output: 14:30:45
Getting Time Attributes
from datetime import time t = time(14, 30, 45) print(t.hour) # Output: 14 print(t.minute) # Output: 30 print(t.second) # Output: 45
datetime.datetime
Class
The datetime
class combines date and time.
from datetime import datetime # Create a datetime object dt = datetime(2023, 7, 20, 14, 30, 45) print(dt) # Output: 2023-07-20 14:30:45
Getting Current Datetime
from datetime import datetime now = datetime.now() print(now) # Output: Current date and time
Getting Datetime Attributes
from datetime import datetime dt = datetime(2023, 7, 20, 14, 30, 45) print(dt.year) # Output: 2023 print(dt.month) # Output: 7 print(dt.day) # Output: 20 print(dt.hour) # Output: 14 print(dt.minute) # Output: 30 print(dt.second) # Output: 45
datetime.timedelta
Class
The timedelta
class represents the difference between two dates or times.
Creating a Timedelta
from datetime import timedelta # Create a timedelta object delta = timedelta(days=10, hours=5, minutes=30) print(delta) # Output: 10 days, 5:30:00
Performing Date Arithmetic
from datetime import timedelta from datetime import date d = date(2023, 7, 20) delta = timedelta(days=10, hours=5, minutes=30) new_date = d + delta print(new_date) # Output: 2023-07-30 diff = new_date - d print(diff) # Output: 10 days, 5:30:00
Formatting and Parsing Dates
Formatting Dates
from datetime import datetime dt = datetime(2023, 7, 20, 14, 30, 45) formatted_date = dt.strftime("%Y-%m-%d %H:%M:%S") print(formatted_date)
Parsing Dates
from datetime import datetime date_string = "2023-07-20 14:30:45" parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") print(parsed_date) # Output: 2023-07-20 14:30:45
Summary of Functions
date(year, month, day)
: Creates a date object.date.today()
: Returns the current date.time(hour, minute, second)
: Creates a time object.datetime(year, month, day, hour, minute, second)
: Creates a datetime object.datetime.now()
: Returns the current date and time.timedelta(days, hours, minutes)
: Creates a timedelta object.strftime(format)
: Formats a date or time as a string.strptime(date_string, format)
: Parses a string into a date or time object.