Python provides a built-in module called datetime for working with dates and times. This module allows you to create datetime objects, manipulate them, and format them according to your needs.
Getting the Current Date and Time
To get the current date and time, you can use the datetime module.
datetime.now(): Returns the current local date and time.
from datetime import datetime
# Get the current date and time
now = datetime.now()
print("Current date and time:", now)Creating Date Objects
You can create a date object by using the date class from the datetime module.
date(year, month, day): Creates a date object representing the specified date.
from datetime import date
# Create a date object
birthday = date(1990, 5, 15)
print("Birthday:", birthday)Creating Time Objects
To create a time object, use the time class from the datetime module.
time(hour, minute, second): Creates a time object representing the specified time.
from datetime import time
# Create a time object
time_of_day = time(14, 30, 15)
print("Time of day:", time_of_day)Formatting Dates and Times
You can format date and time objects using the strftime method, which converts them into a string representation based on a specified format.
strftime(format): Returns a string representation of the date and time object, formatted according to the specified format.
from datetime import datetime
# Get the current date and time
now = datetime.now()
# Format the date and time
formatted_date_time = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date_time)Parsing Dates and Times
You can parse a string representation of a date and time into a datetime object using the strptime method.
strptime(date_string, format): Parses a string representation of a date and time into adatetimeobject based on the specified format.
from datetime import datetime
# Parse a string into a datetime object
date_string = "2023-11-25 11:00:00"
parsed_date_time = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed date and time:", parsed_date_time)Performing Date Arithmetic
You can perform arithmetic operations on datetime objects using the timedelta class.
timedelta(days, seconds, minutes): Represents a duration for performing date arithmetic.
from datetime import datetime, timedelta
# Get the current date and time
now = datetime.now()
# Add 10 days to the current date
future_date = now + timedelta(days=10)
print("Date after 10 days:", future_date)Python Dates Example Code
- Get current date and time: Uses
datetime.now()to get the current date and time. - Create date and time objects: Uses
dateandtimeclasses to create specific date and time objects. - Format date and time: Uses
strftimeto format date and time into a string. - Parse date and time: Uses
strptimeto parse a string into adatetimeobject. - Date arithmetic: Uses
timedeltato perform arithmetic operations on dates.
from datetime import datetime, date, time, timedelta
# Get the current date and time
now = datetime.now()
print("Current date and time:", now)
# Create a date object
birthday = date(1990, 5, 15)
print("Birthday:", birthday)
# Create a time object
time_of_day = time(14, 30, 15)
print("Time of day:", time_of_day)
# Format the date and time
formatted_date_time = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date_time)
# Parse a string into a datetime object
date_string = "2023-11-25 11:00:00"
parsed_date_time = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed date and time:", parsed_date_time)
# Add 10 days to the current date
future_date = now + timedelta(days=10)
print("Date after 10 days:", future_date)


