Tutorial | Python | Datetime

Working With Dates in Python

They aren’t so hard once you learn the basics

Drew Seewald
11 min readMar 11, 2024

--

A clock, the cause of all my problems | Image generated by the author

For those of us unfortunate enough to still be impacted by daylight saving time, this weekend undoubtedly wrecked havoc on our programming processes. Dates can just be so incredibly frustrating to work with if you aren’t familiar with all of the tools at your disposal in Python to handle them. Today I want to look at how to handle common tasks with date, time, and datetime objects in Python.

What is a datetime?

In Python’s standard library, the datetime module has classes that allow storing and manipulating of dates and times. There are 3 types that you may encounter: datetime, date, and time. For our purposes here, I’ll mainly be working with datetime objects. datetime objects contain both date and time parts, so most methods will be applicable on those as well.

datetime objects can do more than just store dates and times. They can also be used to parse strings into datetime objects, set and get components of a datetime, format a string from a datetime object, handle timezone information, and do math with datetimes. We’ll start with how to make a datetime object.

Creating a datetime

--

--