Date Time Module
Date Time Module
The datetime module in Python provides classes and functions for working with dates, times, and time intervals.
It offers a wide range of functionality to handle various operations related to dates and times. Here are some key
features and functionalities of the datetime module:
1. Date and Time Objects: The module provides classes like `date`, `time`, and `datetime` to represent dates,
times, and combined date and time values, respectively.
2. Current Date and Time: You can get the current date and time using the `datetime.now()` function.
current_datetime = datetime.datetime.now()
print("Current Date and Time:", current_datetime)
3. Specific Date time: The datetime module enables you to create specific dates or times using the datetime
class constructor. You can specify the year, month, day, hour, minute, second, and microsecond values. Here's
an example of creating a specific date:
specific_date = datetime.datetime(2023, 5, 31)
print("Specific Date:", specific_date)
4. Date and Time Formatting: The module supports formatting and parsing of dates and times using the
`strftime()` and `strptime()` functions, respectively. These functions allow you to specify the desired format of the
date and time strings.
formatted_date = specific_date.strftime("%Y-%m-%d")
print("Formatted Date:", formatted_date)
5. Date Arithmetic: The datetime module allows you to perform arithmetic operations on dates and times, such
as adding or subtracting days, months, or years from a given date.
specific_date = specific_date + datetime.timedelta(days=1)
print("New Date:", specific_date)
6. Timezone Handling: You can work with different timezones using the `timezone` class and perform
conversions between different timezones.
7. Time Intervals: The module provides the `timedelta` class to represent time intervals, allowing you to perform
calculations involving durations and differences between dates and times.
Here's an example that demonstrates some basic usage of the datetime module:
# To start using the datetime module, you need to import it into your Python script. You can do this by
including the following line at the beginning of your code:
import datetime
# Get the current date and time
current_datetime = datetime.datetime.now()
print("Current Date and Time:", current_datetime)
# Format the date and time
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date and Time:", formatted_datetime)
# Create a date object
date_obj = datetime.date(2022, 6, 15)
print("Date:", date_obj)
# Create a time object
time_obj = datetime.time(9, 30, 0)
print("Time:", time_obj)
# Create a datetime object
datetime_obj = datetime.datetime(2022, 6, 15, 9, 30, 0)
print("Datetime:", datetime_obj)
# Perform date arithmetic
future_date = date_obj + datetime.timedelta(days=7)
print("Future Date:", future_date)
# Convert timezone
utc_datetime = datetime_obj.astimezone(datetime.timezone.utc)
print("UTC Datetime:", utc_datetime)
These are some functionalities offered by the datetime module. The datetime module is a powerful tool for
handling date and time-related operations in Python and provides a rich set of features to work with various
aspects of dates, times, and timezones.