Join Joe Marini for an in-depth discussion in this video The date, time, and datetime classes, part of Learning Python.
- [Instructor] Now that we've taken a look at the basics of the Python language, we're going to turn our attention to using the rich library of predefined code that comes with Python in order to build functionality into Python apps. In this chapter, we're going to focus on manipulating dates and times. So here in my project, I'm going to go to chapter three and open up dates_start. So in order to get this rich functionality that Python provides into your app, you have to tell Python where to go get it from.
The way to do that is by using something called an import statement. So let's add that to our example. So right here at the top, above my main function, I'm going to write from datetime import date. And then I'll paste that couple times, and I'll also import time. And finally, I'll import datetime. So, what I'm doing here is telling the Python interpreter that from the datetime standard module that comes with the standard library, I want to import the date, time, and datetime classes.
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. I just have to use it. And by using it, I need to import it. So, now, let's go ahead and exercise some of the features of these classes. For the first example, let's just print out today's date. So I'll write today equals date and then dot. This gives me access to the functionality that's inside the date class that I imported right here on line four.
So I'm going to write date.today, and I'll call that function, and then I'll write print Today's date is, and then today. 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 this. And you can see that it prints out today's date. So today is 2017, and it's November 8th.
For the next example, let's print out the individual components of a particular date object. So here I'll write print Date components, and it'll be today.day, today.month, and today.year. All right. So, the object that comes back from the date.today function has several properties associated with it. So I can get the individual day, the individual month, the individual year, among other properties.
So once again, we'll run this. And you can see that the components of today's date are being printed. The eight, 11, and 2017. The date object also provides some useful properties that I can use in other more advanced features of an app. So for example, I can retrieve what's called the week day number. And that starts off at zero for Monday and goes up to six for Sunday. So what I'm going to do here is print Today's weekday number is and then today.weekday.
And that's a function, so I'll call it that way. And once again, I'll run. And you can see that the weekday number for today is two. So if I had some list that I wanted to provide an index variable for that depended on the weekday, I could use the weekday property to index into supposing a list of abbreviated weekday names. So let's go ahead and write that, and a variable here. This is days is equal to, and I'll just have Monday, Tuesday, Wednesday, Friday, Saturday, and Sunday.
And then I can do something like print, which is a, and then I can index into that array with today.weekday. Alright, so now let's refresh and try that again. And you can see says now which is a Wednesday, which it is. Alright, that's a pretty good introduction to date objects. So let's try working some datetime objects.
Just like working with dates, I can get time as well. So, using the datetime class, and actually before I do any of that, let me just comment out these lines. All right. So, using the datetime class, instead of a date class, I can call the now function, and that will give me the current date, as well as the current time. So I'll write today equals datetime.now. And I'll just print that out.
Alright, so let's save and let's run. And you can see that this is the date right here, and this is the current time. We've got hours, minutes, seconds, and milliseconds. So to get the current time, we need to get the time portion of the datetime object. So let's do that. All right. T equals datetime.time. And I'll give that the datetime.now result.
So this will take a now object from the datetime class, pass it into the time function, and that will just give us the time. I'm going to print it. We'll save and run it. And now you can see that I've got the date and time, and I've got the time just by itself.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML