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.

Introducing datetime

In this lesson you’ll try your hand at using the datetime module. Along the way, you’ll learn how to:

  • Get the Unix Epoch
  • Build date objects
  • Build datetime objects
  • Combine time and date objects

00:00 In the previous lesson, I explained how complicated dates and times could be. In this lesson, I’ll introduce you to the main Python module for dates and times, datetime.

00:11 There are three standard libraries in Python for dealing with dates and times: calendar, time, and datetime. Let’s go play with some of these in the REPL.

00:24 I’ll start out with the time library. Let me import it and then call the time() function inside of it. This returns the number of seconds since the Unix epoch.

00:37 Remember how I mentioned the 32-bit storage problem? Well, the maximum integer in 32θ storage is 2.1 billion. That number right there—that’s starting to get pretty close, just over 500 million seconds to go. Python has date, time, and datetime modules. As the datetime module handles both dates and times, it is preferred to use that for both cases. Let me import it.

01:05 You can create a date by specifying the year, month, and day as arguments …

01:15 or a time by specifying the hour, minutes, and seconds …

01:24 or a datetime by specifying all those things.

01:34 The date class has a class method that returns today’s date.

01:44 That was February 20, 2022, when I recorded this. The datetime class has something similar, called .now().

01:58 Normally it is my jokes that date me, but code on the screen is a rather specific point in time. You can access the different parts of a datetime object using attributes with the same names as the arguments to the initializer.

02:19 Here, I’ve created a time object using the .hour, .minute, and .second attributes of the datetime object previously created and stored in the now variable.

02:30 The datetime class also has a constructor called .combine(), which creates a new datetime object by combining a date object with a time object.

02:45 You can also create datetime objects by parsing strings. This is particularly useful if the information you have isn’t in standard ISO 8601.

02:54 That’s the official name of the year-month-day-et cetera format. Here’s the date and time in the American format … and now a format specifier …

03:12 and passing them both to datetime’s .strptime() (string parse time) method …

03:23 and you get a resulting datetime object, or an exception if you mucked something up, like forgetting a percent sign, like I did the first three times I recorded this.

03:35 Let’s dig in just a little bit more to that mini-language of percent signs that specify a format string. The parsing format I just showed you is part of a mini-language that specifies all sorts of different ways of expressing date and time things.

03:51 This table shows some of the more common ones. The mini-language is based on the corresponding 1989 C language standard that does the same thing. That’s also why the function is named strptime() instead of something pronounceable.

04:06 There’s a corresponding string format time, strftime(), for taking a datetime object and generating a formatted string using the same specifiers.

04:17 This mini-language is pretty common across a lot of different languages. You’ll find it in PHP and Perl, for example. Speaking of parsing dates and times, a popular third-party library for parsing dates is called dateparser. It includes the ability to parse human-readable datetime concepts like yesterday or morgen, which is tomorrow in German. Throughout this course, I’m going to show the steps to building a little utility that uses some of the datetime concepts I’m talking about.

04:49 You may remember there was a big fuss that the world might end in December of 2012 due to the rollover of the Mayan calendar. The Mayan long-count calendar has a 5,125-year cycle in it, made up of thirteen segments called baktun. On December 12, 2012, the thirteenth baktun came to an end.

05:11 When the millennium changed over, everyone was going to party like it was 1999—see Prince, artist formerly known asbut can you imagine what it’d be like to party like it was the thirteenth baktun?

05:23 Anyhow, the code in the top window tells you how long you have survived since the so-called end of the world. Is it the end of your car when the odometer flips over? People are strange.

05:35 Line 4 creates a datetime object specifying the last second of the thirteenth baktun. Line 5 calculates the difference between right now and the baktun. Finally, line 7 prints out the result.

05:51 Let’s run this in the console. And there you have it: 3,347 days and some bit. Party on, dude. You’ve seen the simple form of Python’s datetime object. Next up, I’ll augment that with some time zone information.

Become a Member to join the conversation.