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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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

Download

Sample Code (.zip)

3.8 KB

Download

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.

Become a Member to join the conversation.