From the course: Learning Python

The date, time, and datetime classes - Python Tutorial

From the course: Learning Python

The date, time, and datetime classes

- [Instructor] In this chapter, we're going to focus on manipulating dates and times. And we're going to start off by learning about some of the basic classes and types that are available for this purpose. So here in my project, I'm going to go to the chapter for dates and times and open up dates_start.py. And let me go ahead and shrink that down. All right. So, in order to get this rich functionality that Python provides your application, you have to tell the Python interpreter to go get it from somewhere. And the way that you do that and we've seen this a little earlier in the course, is by using something called an import statement. So let's go ahead and add that to our example. And what I'm going to do is rather than import everything from date times module, I'm going to write from datetime import date. And I'll do the same thing from datetime import time and then from datetime import and this may be a little bit confusing but there's actually a datetime class inside the datetime module. So I'm going to go ahead and import that as well. So what I'm doing here is telling the Python interpreter, that from the datetime standard module, I want to import the time, date and datetime classes. And these are predefined pieces of functionality in the Python library that let me manipulate dates and times. So I don't have to write this code, it's already written for me and I just need to import it in order to use it. So let's now go ahead and exercise some of the features of these classes. So for the first example, let's just print out today's date. So we are going to write today equals date.today. And then I'll print a string that indicates today's date and the today variable, right? So this code calls the today method on the date class which returns information about the current date. So let's go ahead and run that and see what happens. And I'll just choose run Python file in terminal from visual studio code. Of course I can also do that up here, so I'll choose that. And you can see that today's date is 2021, October 4. All right, so far so good, let's close this terminal. So for the next example let's print out some of the individual components of a particular date object. So using the today variable, I'm going to print out (indistinct) string says date components and then I'm going to print today.day, today.month and today.year. So the object that comes back from the today function has several properties associated with it. I can get the individual day, the individual month, the individual year among other things. So now let's go ahead and save this and run it again. And you can see in the output that the day components here is the day, here's the month and here is the year. All right. So the date object also provides some other useful properties that I can use in other more advanced features of an application. So for example, I can retrieve what's called the weekday number. And that starts off at zero for Monday and goes up to six for Sunday. So let's go ahead and try that out. I'll print that today's weekday number and I'll access today.weekday. So let's imagine I had some list variable and I want it to provide an index into that list that depended on the weekday. So I can have something like days and then days could be a series or a list of strings like Monday, Tuesday, right, Wednesday, Thursday, Friday and then we'll just do the weekend as well. So what I can then do is something like this. I can print which oops, which, inside quotes, which is a, and then I'll print out, I put a space there as well, I'll print out the day's list and I'll index into the day's list using today.weekday. All right, so let's go ahead and run that. And you can say that today's weekday number is zero because it's a Monday and sure enough when I index into that list it says which is a and then the abbreviated Monday. All right, so that's a pretty good introduction to working with date objects. Let's try working with some datetime objects. So just like working with dates, I can get times as well. So using the datetime class instead of the date class, I can call the now function. And that will give me the current date as well as the current time. So let me comment out these previous examples and I'll use control slash for that or command slash on the Mac. And let's run what we have. And you can see that it says the current date and time is, and we got 2021-10-04 and then I've got the time, I've got hours, minutes, seconds, and even some milliseconds. Right, let's close this terminal. So to get the current time, we need to get the time portion of the datetime object. So I'm going to have a variable named t and I'm going to call datetime.time. And then I'll pass the datetime.now's result into that. And then I'll print the current time is and then the t variable, all right? And the datetime class remember we imported that up here in the import list. So that's the item in the top in the import and that's this object I'm going to be using right here. So I can now create a class, a timeclass object and give it the value of datetime.now. And that will give me just the time. So let's go ahead and run this so we can compare it side by side. And when I run this, you can see in the output, it says the current date and time, that's what we got before. And now it says the current time and it shows me just the time now.

Contents