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