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 (Overview)

Python has several different modules to deal with dates and times. This course concentrates on the primary one, datetime. Dates and times are messy things! Shifts due to daylight savings time and time zones complicate any computing with dates and times.

In this course, you’ll tackle that messiness and learn:

  • How to use the datetime module
  • What the zoneinfo module does and how to use it
  • How to calculate the difference between two datetime objects

Download

Sample Code (.zip)

3.8 KB

Download

Course Slides (.pdf)

921.6 KB

00:00 Welcome to Using Python’s datetime. My name is Christopher, and I will be your guide. This course gives an introduction to the messy world of managing dates and times in software. As the title implies, it primarily focuses on Python’s datetime module, but also shows some other things along the way.

00:19 In this course, you will learn about how messy dates, times, and time zones are, the datetime module, time zones in Python, and how to do date and time math. Code in this course was tested using Python 3.10.

00:36 Time zone handling changed significantly in Python 3.9 when things previously managed by third-party libraries were brought into the standard library. The changes were significant enough that they were backported to older versions. If you’re using something prior to Python 3.9, I’ll point you at the backports when I get there.

00:56 Python has three main modules included as part of the standard library for dealing with dates and times. The main one you probably want to use is datetime, which also includes tools for date objects, time objects, and time deltas, although this one’s a bit rudimentary. Dates and times are messy things.

01:16 There are loads of formats out there. As a Canadian, I can tell you that I get bit by this all the time. Our American neighbors use a different short form when they write dates.

01:25 The difference between day-month-year versus month-day-year can be unclear up until the thirteenth of the month. As a programmer, I try to insist on year-month-day, the international standard, but standard or not, it isn’t always used. Time zones can also cause craziness.

01:44 There are more times zones in the world than the twenty-four hours in the clock. That right there tells you something. Not to keep bringing up the Canadian thing, but it is another good example of the mess. Canada has six times zones, one of which, Newfoundland Standard Time, has a half-hour difference from its nearest neighbor. Combine that with the problem of daylight savings time, which is also messy.

02:05 One of our provinces, Saskatchewan, doesn’t follow daylight savings. So in the winter, it aligns with Central Standard Time and has the same time as its neighbor to the east, Manitoba, while in the summer, it aligns with its neighbor on the other side, Alberta, as it syncs with Mountain Standard Time.

02:24 Take off, eh, there’s no doubt about it: date and times are messy.

02:29 Now that I’ve slagged my fellow Canadians, next up I’ll dive further into all the complications of dates and times.

Become a Member to join the conversation.