Tutorial

Datetime Module in Python

2 min read

datetime module in Python is a library that provides various classes and functions for working with dates, times, and time intervals.

It allows us to easily manipulate, format, and calculate dates and times. The datetime module is part of Python’s standard library, so we don’t need to install any external packages to use it.

The datetime module includes several classes, with the primary ones being datetime, date, time, and timedelta.

Datetime Class in Python

datetime class: This class represents a specific date and time.

It includes information about the year, month, day, hour, minute, second, and microsecond.

We can create a datetime object using the datetime() constructor and access its components using various attributes and methods.

from datetime import datetime

now = datetime.now()  # Current date and time
print(now)
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.microsecond)

Date Class in Python

date class: This class represents a date without the time components. It includes information about the year, month, and day. We can create a date object using the date() constructor and access its components using attributes and methods.

from datetime import date

today = date.today()  # Current date
print(today)
print(today.year)
print(today.month)
print(today.day)

Time Class in Python

time class: This class represents a time without the date components. It includes information about the hour, minute, second, microsecond, and optional time zone information. We can create a time object using the time() constructor and access its components using attributes and methods.

from datetime import time

current_time = time(hour=10, minute=30, second=45)
print(current_time)
print(current_time.hour)
print(current_time.minute)
print(current_time.second)

Timedelta Class in Python

timedelta class: This class represents a duration or difference between two dates or times. It allows us to perform arithmetic operations on dates and times. We can create a timedelta object using the timedelta() constructor and use it to add or subtract time intervals from datetime objects.

from datetime import timedelta

one_day = timedelta(days=1)
yesterday = datetime.now() - one_day
tomorrow = datetime.now() + one_day
print(yesterday)
print(tomorrow)

Formatting date & time using Python Datetime Module

The datetime module also provides functions for formatting dates and times as strings (strftime()) and parsing strings into datetime objects (strptime()).

from datetime import datetime

current_time = datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_time)

parsed_time = datetime.strptime("2023-06-18 15:30:00", "%Y-%m-%d %H:%M:%S")
print(parsed_time)

The datetime module offers various other functionalities, such as comparing dates and times, extracting specific components from a datetime object, and working with time zones. It is a versatile and essential module for any Python developer working with dates and times.

You may also like to get a personal Python tutor for yourself to learn in private online sessions.


Leave a Reply

Your email address will not be published. Required fields are marked *