Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 3: Birthday countdown app
Lecture: Differences between dates

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we're getting the birthday from the user, the next thing to do is to figure out how close that is to today it's
0:10 good to hear an uncommon this remember this was crashing and if we run it it's
0:14 still going to crash saying a compute days between dates takes zero positional arguments but two
0:21 or given that means it's unhappy that we're passing these things to it but it doesn't
0:25 appear state that it expects any parameters some languages like java script we'll let you get
0:29 away with this Python so let's go over here and say we're going to have these two things passed in date one and dates you now how do i given two dates
0:38 get the difference between them? Well, if these were numbers, you would just say something like this we just subtracted so
0:47 the difference is gate one money states you and it turns out that this also works just fine and pipe on hand if we print out the type here we were on
0:56 this put some random numbers didn't matter oh yes so it will work when we actually
1:02 pass the value. So first thing let's we're passing now is none and none is not a date you can work with that this is this now is supposed to represent
1:12 today could even rename it to today potentially so what kind of hearsay daytime dot date and then how do you get that?
1:19 Well, you could enter these times are the date, but if you're just looking for the day you could say today right and this will
1:27 return a new date which is just today so this will work but i kind of would like to think of this is today instead of now,
1:34 so call it today dual re factor in Python all right now let's, go back and look at this what we get when lisa track these two so that's
1:43 a lot of days but notice that we have a time delta so this attraction things works here and how do we figure out how many days we can go to this?
1:52 I say dt dot, what do we got? We have day we had days here we also have a month a second,
2:00 things like that, but what we care about is days and which want to return this here was not quite right.
2:08 We'll see there's something a little bit wrong with this but let's, get rid of this printing of the birthday and let's print out just the number of
2:15 days so we can see how many days have passed today is september eighth,
2:21 two thousand seventeen let's say i was born nineteen seventy four let's say we'll say august
2:28 first so should be like forty days or something like that in the past fifteen thousand
2:33 seven hundred forty four that seems a little different than what i was expecting what has happened this date was first day is the birthday right?
2:41 And the birthday is in nineteen seventy four this one his in twenty seventeen so when we subtract him it gives us this huge number for the day,
2:51 so we need to put them basically in the same year what we're really trying to do is say given a particular calendar year,
2:57 how many days it between those two places on that one one year so in some sense you might think what we could take this state which is nineteen,
3:05 seventy four and moving up to two thousand seventeen or this one and move it back
3:08 to nineteen, seventy four and do that's attraction but really got to consider that might
3:12 be a leap year. So what we want to do is upgrade or culture this
3:16 date and make it move to twenty seventeen so it's renamed these parameters a little bit
3:21 to just make life a little bit easier for so we'll call this original date and this will call target date. So i'll say this year's date is going to be
3:31 a daytime don date and then we can set the year equal to the target date
3:36 here but then everything else tio original spell original writes better actually it's interesting that pie
3:43 charm we'll find spelling there's just look it would have told me ok original date here
3:52 we want month and we could shorten this up just so if it's on the screen dot day okay so now this year is the same month and day as the birthday
4:08 but it's in the current year not in the past so then i could say this
4:13 year minus target data now we returning this should be something closer to reasonable to nineteen
4:20 seventy four let's go with august first thirty eight days so that is thirty eight days
4:28 in the past meaning we already had our birthday thirty eight days ago okay great.
4:32 What are we going to do with that information last thing to do instead of printing out that is to come over here and print to eight days oh my god well
4:41 come on hand print out something like your birthday's coming in thirty days or you just
4:46 had your birthday or things like that so we're going to come over here we have a little test will say if days is less than zero the way we computed the
4:54 difference that means it was thirty eight days in the past so we'll say print something
4:58 like this all right so you have your birthday some number of days ago and we'll just do a format and pass the days on the other hand will say else if
5:07 so l if days is greater than zero that means your birthday is coming up we'll put a days. And if it's not bigger, it's, not less than zero.
5:16 It must be zero. So we can say else today is your birthday perfect let's try this again. And my charm has told me there's a misspelling of this
5:25 word. But you know what hard is going to go with? Ok, so let's run this nineteen seventy four i was born august first. Let's say. And that is,
5:36 i think, thirty eight days ago. Let's. See you had your birthday. It was negative thirty eight days ago. So that doesn't make a lot of sense,
5:43 right? Let's negate this here just for the display purposes nineteen, seventy four eight one. You had your birthday thirty eight days ago.
5:53 And because it's september eighth. That sounds right to me. Let's. Run it again. So nineteen seventy four let's.
5:59 Say it's nine. Ten. So it should be in two days. Yeah, your birthday's in two days. All right. You ready for starting the party?
6:08 Nineteen seventy four nine enter today and we get happy birthday. All right, cool. So we have all the three different cases in the future
6:17 in the past and and even today.

Talk Python's Mastodon Michael Kennedy's Mastodon