Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Python's datetime Module (Summary)

In this video course, you learned about programming with dates and times and why it often leads to errors and confusion. You also learned about the Python datetime and dateutil modules as well as how to work with time zones in your code.

In this course, you learned about:

  • The messiness of dates and times
  • The nuts and bolts of the datetime module
  • The addition and subtraction of time from a datetime object
  • The calculation of how long it has been since the Bak’tun

For more information on concepts covered in this course, you can check out:

Other Third-Party Date/Time Libraries


Sample Code (.zip)

3.8 KB

Course Slides (.pdf)

921.6 KB

00:00 In the previous lesson, I showed you how to find the differences between datetime objects. This lesson summarizes the course and points you at some further information.

00:11 I started out with the warning of how messy dates and times can be and then showed you how to use four different Python standard libraries for date and time objects: time, datetime, zoneinfo, and timedelta. As the timedelta class is a bit limited, I showed you the dateutil package as a possible augment.

00:34 And throughout the course, I added to a script that calculates how much time has passed since the last apocalypse.

00:42 In the description below, I’ve included several different articles about the messiness which is working with dates and times. Your Calendar Fallacy is a long list of things that people often think are true about dates, and as you might guess from the title, well, they’re not. This Stackoverflow discussion has an excellent answer summarizing a lot of the best practices with daylight savings and time zones.

01:07 In the overview, I hinted that the Let’s use UTC to store things solution isn’t always the right one. This article talks about some of the edge cases … and this is another perspective on the same topic.

01:22 This YouTube video from Computerphile covers a lot of similar topics if you’d rather watch than read. And finally, a comprehensive article on all this messiness.

01:35 Because Python’s date/time libraries are a little lightweight, there are loads of third-party libraries out there to compensate. Links to these projects are in the description below. pytz was the solution to time zones in Python before zoneinfo existed.

01:51 I’d suggest using the standard-library solution instead, but this one is definitely still out there in the wild, so you might want to be familiar with it. Now, here are three different drop-in replacements for datetime.

02:04 They provide compatible methods as well as all sorts of other capabilities. Arrow, Pendulum, and interestingly enough, Maya.

02:16 In a previous lesson, I mentioned dateparser, used to parse human-readable time concepts, like tomorrow, and having some of my childhood overlapping with the eighties, the name of this module always just makes me smile.

02:29 This one, Delorean, mixes the human-readable concepts of dateparser with the time delta concepts of dateutil. Finally, pywhen. Full disclosure: this is one of mine.

02:40 It’s a very lightweight library that just wraps the construction of datetime objects. I was doing some work a while back where fractional sections and Unix epochs were important and found nothing else did what I needed. So I threw this together.

02:56 Thanks for your attention. I hope you had a good time. See what I did there? Dad joke for the win.

Avatar image for mckown

mckown on May 24, 2022

Fascinating course. I’ve struggled with dates and times for years. This course helped to clarify some points. One problem I’m currently struggling with–which isn’t covered in this course–is rolling over calendar events to another year/term/whatever to the same weekday as previous. Hence, an event scheduled for Monday, April 4, 2022, should be rolled over to Monday, April ?, 2023. I’ll get it figured out, but this course gives me lots to explore. Thanks!

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on May 24, 2022

Glad you liked it mckown,

Yeah, dates are messy messy things. A couple of the libraries mentioned on the “More Date/Time” slide support things like “tomorrow” as an action. IIRC, they may even support “3rd Monday”. So you take Monday April 4th, figure out how many Mondays in it is, then use the library to get “1st Monday” in the following year.

You’ll need some corner-case handling code, because there are going to be months where there aren’t the same number of Mondays from year to year, but you can probably just default to the week before if you detect that the suggestion pushes you into May.

Happy coding!

Avatar image for billteale

billteale on July 11, 2022

I really enjoyed the course, and being a dad joke aficionado I think you really ended on a high note!

Avatar image for Christopher Trudeau

Christopher Trudeau RP Team on July 12, 2022

Glad you enjoyed the course and the humour @billteale. I can only promise quality in one of those two areas :)

Avatar image for akazimierz

akazimierz on Aug. 29, 2022

Thanks for this course – the idea is quite messy, indeed. Mastering the datetime takes some practice, for sure.

Avatar image for tonypy

tonypy on May 1, 2023

Very interesting course, like your presentation style. A couple of issues to report on time zones. On Windows, setting

london_tz = ZoneInfo("Europe/London")

results in an error

zoneinfo.common.ZoneInfoNotFoundError: ‘No time zone found with key Europe/London’.

I think the reason is that explained on the site @ docs.python.org/3/library/zoneinfo.html#data-sources.

So I tried the following

from dateutil import tz
from datetime import datetime
london_tz = tz.gettz("Europe/London")

The result of

londown_now = datetime.now(tz=london_tz)

came back as

datetime.datetime(2023, 5, 1, 13, 29, 40, 612275)

which seems ok. Unfortunately, the result of

londown_now = datetime.now(tz=london_tz)

came back as

datetime.datetime(2023, 5, 1, 13, 12, 27, 743881, tzinfo=tzfile(‘Europe/Belfast’))

How did the system translate between ‘Europe/London’ and ‘Europe/Belfast’ which I think has been obsoleted in favour of London. Very odd.

Become a Member to join the conversation.