Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Dates and Times!
You have completed Python Dates and Times!
Preview
We walk through the creation of a crucial function, `display_age()`, for the Birthday App. We'll utilize Python's `datetime` tools to handle age calculations.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's work on displaying age first,
0:00
because this is actually
the easiest function.
0:02
And we can use what we write here for
some of our other functions as well.
0:06
It looks like the developer
has left us a to do.
0:11
So write code to display
the age of person.
0:14
And they've also left us a template person
is x years x months and x days old.
0:16
Awesome, and we got some print statements.
0:23
We've got this display h function just to
sort of say that we're in here now in this
0:24
function.
0:28
And then we also printing out this person
variable that we're getting through
0:29
this argument right here.
0:33
So let's just run the app and
run this function to see what we get.
0:34
So python3main.pi and since we're
displaying an age this will be number two
0:39
check someone's age and
let's just select Arby for example.
0:43
Okay, so we get the display age function
print line which is to be expected and
0:48
then in our person variable we're
getting this dictionary where name
0:53
is the person's name.
0:57
And then birthday is their birthday in
a date string that goes year-month-day.
0:59
Now this is important to remember because
we will need to know what format this date
1:05
string is in so that we can use it.
1:10
So our job is pretty simple,
1:12
we need to turn whatever data is in
this dictionary into this template.
1:14
So let's grab the template and
fill out what we can.
1:20
So I'll just replace this print line with
our template and change it to an F string.
1:24
Make this just a little bit smaller.
1:29
Okay.
1:32
Now we already know the person's name.
1:33
The person's name is provided right
here under the name attribute, Arby.
1:35
So let's just pop that right in,
it's a person and name.
1:41
Now we'll just need to work out the years,
months and
1:46
days between two dates,
their birthday and today.
1:50
So that seems like a pretty good time for
us to use relativedelta.
1:54
So in order to use relative delta,
we'll need to import it, so
1:59
let's make sure we do that.
2:01
Now we already installed
relativedelta in the last video.
2:03
So we should just need to
from dateutil import relativedelta.
2:06
And of course, we'll might as well import
datetime now since we'll definitely
2:15
be using it and we'll give it that
extra space and coming back down here.
2:20
Okay, so the first thing that I would
do is to turn this date string into
2:25
a format that we can use,
which is a datetime object.
2:30
So let's go ahead and
do that with string parse time.
2:34
Remember that's the function that takes
a date string, and then a format and
2:37
then it turns it into
a datetime object for us.
2:42
So let's start with the string format so
the format string I mean,
2:45
now we know that it goes year-month-day.
2:51
Now remember, you can always look
up the different format codes.
2:54
In fact, I don't recommend that you
try to remember everything at all.
2:58
So I've got mine in front of me.
3:02
So I know that this is Y, m and
3:04
d that matches the birthday string format.
3:08
Okay, so now let's use string parse time
and then we'll save it into a variable.
3:14
So we'll just call it the birthday
date time, equals datetime.datetime
3:18
string parse time,
now I'll press Enter here to autocomplete.
3:24
Now the first thing that we have to input
is our date string, now if you're using
3:29
PyCharm like I am, you'll get this little
helpful handy hint that appears.
3:35
And this is quite useful just in case you
forgot which order they're supposed to
3:41
come in it's right there for
you don't have to look it up.
3:44
So looks like the first
thing is our date string, so
3:48
I will simply put in person birthday.
3:52
And then the second thing
is our birthday close, and
3:55
the second thing is our format string
which I've got format string right there.
3:59
Okay, so this birthday_datetime
should contain a datetime
4:06
object of this person's birthday.
4:11
Now we'll also need today's date.
4:15
So let's create a today variable.
4:17
And then we'll simply use
datetime.date's today method.
4:20
Okay, with those two
datetimes ready to go,
4:26
we just have to find
the difference between them.
4:28
So using relative delta,
4:31
we'll call it difference equals
relative delta.relative delta.
4:33
Now I'll put today first because
this has to be the earlier datetime.
4:40
And then birthday datetime second.
4:44
Okay, so let's just print out difference
just to see what we've got just to make
4:49
sure that we are doing the right thing.
4:52
So back over here in our terminal, we'll
exit the app with the quit the app option,
4:54
and then we'll just reload it again.
4:59
We'll check someone's age was two.
5:02
Let's check Arby again.
5:04
And we are getting a relative
delta printed out right here is
5:06
a relative delta.
5:09
So Arby looks like, 33 years old
five months and seven days old.
5:12
Okay, so
we've got each one of these ready to go,
5:17
we can plug those straight
into our template.
5:20
So we will comment out this print
statement, and we'll just come
5:24
down here and start replacing all
of these parts of our template.
5:29
So the first is year,
we'll just simply do difference.year.
5:34
Now remember, it is plural years,
5:38
as you can see here in the printout,
there is an S there.
5:40
And then month, no, what am I doing?
5:46
difference.months and difference.days.
5:48
And we'll comment out this
print person statement.
5:55
We don't need that.
5:58
And we also don't need this pass.
5:59
So we should be good to test it out,
let's test it out.
6:02
Let's exit the app, reload the app.
6:05
And let's check out Arby's age again.
6:09
Arby is 33 years 5 months, and 7 days old.
6:11
There you go, we've written our
first function for this project.
6:16
Well done.
6:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up