Home [python] Mastering Dates and Times in Python
Post
Cancel

[python] Mastering Dates and Times in Python

Intro

Introduction

Dealing with dates and times is a common task in programming, and Python provides a powerful module called datetime to handle this. In this blog post, we will explore how to work with dates and times in Python using the datetime module.

Date and Time Representation

In Python, dates and times are represented using the datetime class from the datetime module. This class provides various attributes and methods to work with dates and times. To create a datetime object, we can use the datetime constructor with the year, month, day, hour, minute, second, and microsecond as arguments.

Example 1: Creating a Datetime Object

1
2
3
4
from datetime import datetime

now = datetime(2022, 9, 14, 15, 30, 0)
print(now)

Date Formatting

We can format dates and times using the strftime() method, which allows us to specify a format string to display the date and time in a desired format. The format string consists of format codes that represent various components of the date and time.

Example 2: Formatting a Datetime Object

1
2
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)

Date Arithmetic

We can perform arithmetic operations on dates and times using the timedelta class from the datetime module. This allows us to add or subtract a specific amount of time to a datetime object.

Example 3: Date Arithmetic

1
2
3
4
from datetime import timedelta

tomorrow = now + timedelta(days=1)
print(tomorrow)

Conclusion

In this blog post, we have covered the basics of working with dates and times in Python using the datetime module. By mastering these concepts, you can easily manipulate dates and times in your Python programs. The examples provided demonstrate how to create datetime objects, format dates, and perform date arithmetic. Start incorporating these techniques into your Python projects to handle dates and times effectively.

This post is licensed under CC BY 4.0 by the author.
Contents