Python Date and Time Handling (datetime module)

IHUB Talent – Best Full Stack Python Training Course Institute in Hyderabad

Python is one of the most powerful, beginner-friendly, and in-demand programming languages used in full stack development, data science, machine learning, and automation. If you're looking to build a career in Python development, IHUB Talent is the best Full Stack Python training course institute in Hyderabad. The institute offers a live intensive internship program led by industry experts, ideal for graduates, postgraduates, career switchers, and individuals with education gaps. 

Mastering Date and Time Handling in Python with the datetime Module

Handling date and time effectively is crucial in many software applications — from logging and event scheduling to data analysis and time-based automation. Python simplifies this with its built-in datetime module, offering powerful tools for manipulating date and time objects seamlessly.

What is the datetime Module?

The datetime module in Python provides classes for working with both dates and times in a precise and user-friendly way. The primary classes include:

date: handles year, month, and day.

time: handles hour, minute, second, microsecond.

datetime: combines both date and time.

timedelta: represents the difference between two dates or times.

timezone: handles time zones and UTC offsets.

Getting Current Date and Time

python

from datetime import datetime

now = datetime.now()

print(now)  # Output: 2025-07-09 17:32:10.123456

You can also extract individual components:

python

print(now.year, now.month, now.day)

Creating Custom Date/Time

python

from datetime import datetime

custom_date = datetime(2025, 12, 25, 10, 30)

print(custom_date)

Formatting and Parsing Dates

To convert a datetime object to a string:

python

formatted = now.strftime("%Y-%m-%d %H:%M:%S")

print(formatted)  # Output: 2025-07-09 17:32:10

To convert a string back to a datetime:

python

from datetime import datetime

date_str = "2025-07-01 14:00:00"

parsed_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")

print(parsed_date)

Date Arithmetic with timedelta

python

from datetime import timedelta

tomorrow = now + timedelta(days=1)

yesterday = now - timedelta(days=1)

print(tomorrow, yesterday)

You can also perform operations like calculating the number of days between two dates.

Working with Timezones

python

from datetime import timezone

utc_now = datetime.now(timezone.utc)

print(utc_now)

For advanced timezone support, consider using the pytz or zoneinfo module in Python 3.9+.

Conclusion

The datetime module in Python is a robust toolkit for managing date and time in various formats. Whether you're building a calendar, handling logs, or analyzing time-based data, mastering datetime ensures accuracy and efficiency in your code.

Read More

BeautifulSoup for Web Scraping Basics

Matplotlib and Seaborn: Basic Data Visualization

Visit Our I-HUB Talent Testing Institute Hyderabad

Comments

Popular posts from this blog

Understanding Client-Server Architecture

Building Your First Full Stack Python App

What is Python and Why It's Popular for Full-Stack Development