#100DaysOfCode in Python Transcripts
Chapter: Days 1-3: Playing with Datetimes
Lecture: Learning datetime and date

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Given datetime is part of the Python standard lib, we don't actually have to do any setup here. You'll see in the coming videos
0:10 that you will have to do setup steps, create virtual environments and whatnot, but given this is datetime, we don't really have to.
0:18 And, I think it'd be best for us to just work in the Python shell here. This is IDLE, the default Python IDE that it ships with.
0:28 So, let's have a play with that. Now, the first thing we're going to do is we're going to import datetime. But, we're actually going to do
0:38 from datetime import datetime, okay? And, this is just going to make it a bit easier for us when we're typing in the rest of our code.
0:49 And, just to get yourself prepared, let's just from datetime import date that's for a bit later in this video. Alright so what is datetime, alright.
1:01 For those who are unaccustomed and unaware datetime is just the Python library module that allows you to deal with dates and times.
1:12 Pretty self-explanatory, right? So, if you want to deal with just the dates so, you know, today's date, let's call it
1:22 the 23rd of February 2018, not very specific. Or if you want to deal with the time that you've got to think about that
1:31 from a programming perspective, there is a difference, okay. So, datetime allows us to deal with the entire time set, the entire timeframe.
1:42 You're talking seconds, minutes, hours, days all the way through to years, okay? We can visualize that with datetime.today().
1:53 If we hit enter, there we go, we can see today's date. The 24th of February 2018 but we also get this timestamp. It's 10:17pm
2:06 and these are the extra seconds here. So seconds, milliseconds and whatnot, okay? Now I'm going to show you this, what kind of an object is this?
2:17 Well let's go, well first actually we have to assign that to something that way so, we'll just go with today. Here's datetime.today()
2:29 alright and then we'll type it out, so type today. So it's a datetime object, okay? And that's important because you can't mix these objects.
2:40 I'll point that out in just a minute. So with this timestamp, there is more you can do with that.
2:47 And I'll show you that in the next video with timedelta. Alright, but for now just understand that this is what your
2:54 standard datetime format will look like. This is the sort of data you're going to get back. And this is really useful for times when you want to
3:04 deal with say, subscriptions or anything like that where it has to do with exact timestamps, or logging or anything where you need to know
3:14 the time that something happened. Going by the date of say, the 24th of February is not accurate enough, okay, there is 24 hours
3:23 within that day so, a lot of things could have happened. Alright, so we'll move on to the date part here.
3:31 So we'll just go today date, we'll create that variable. Here's date.today(), so you can see straightaway
3:39 we're not using datetime, we're using the date section okay, we're using the date option here. So date.today() and if we type that out
3:54 Today date, we can see the different type of object here. First one was a datetime and now it's a date object, okay?
4:05 And we can see what that looks like with today date. And we have just the date string, okay? So we don't have the extra time on the end.
4:16 And this is, again, very useful. So you can see the distinction between the two of them. Alright let's get ourselves a little bit of white space.
4:26 Now one really cool thing that I love about date is that we can drill into it a little more, so we can go today.month is 2.
4:42 So you can see we can actually tear it apart a bit. So today.day is 24 and then today.year, and we get 2018.
4:58 So now you can sort of visualize how date can help you in your projects, right, if you're not already using it. It's actually really cool.
5:05 So one really, really cool thing that has come in handy over time, is the fact that you can do a bit of math with your dates, alright.
5:16 So we'll go, let's just go something easy. So Christmas, what's the date for Christmas? It's the, we'll go year first, so 2018.
5:26 It's the month next, so 12. And then it's the day, so 25th, alright. Now one thing, if you had a look, this is ... us specifying a date, this is us
5:43 assigning a date to a variable. So now the Christmas variable is always going to have this date assigned to it. You can see that there, okay.
5:58 Now, this is really cool, so We can actually go Christmas, cause we know that's the end of this year, minus, today date.
6:11 Kay, and that's 304 days, it automatically called on timedelta, so that's giving away something for the next video but, carry on, 304 days.
6:23 Alright, and we can see that visualized a different way. We can, and this is again giving more away we can go Christmas minus today in days, so .days.
6:40 304 days, alright and this is really cool for something such as this, I'm just going to copy and paste here
6:47 rather than type it all out for you, alright. So if Christmas is not today date well what can we do? We can print a certain message.
6:59 Again, you can see this is useful for certain other projects so print, sorry there are still this many days
7:10 (christmas minus today).days, until Christmas. Okay, and then else ... We'll copy and paste this as well.
7:21 We're going to print some sort of message, alright. "Yay, it's Christmas." So, by hitting enter, sorry there are still 304
7:33 the same value here, until Christmas. I've obviously left out the word 'days' so that's my mistake, but sorry there are still 304 days until Christmas.
7:43 If I happen to wait another, you know, ha ha ha ha that many days, 304 days we would then get this message here. So this is date and this is datetime.
7:55 Very, very tedious at times, I want to say but so useful, so this is a great place to start manipulating your code, manipulate your dates
8:07 and have some fun with it. And in the next video we're going to look at datetime.


Talk Python's Mastodon Michael Kennedy's Mastodon