>> All right. Let’s jump into the code
and see how the dates look when we use it inside our code.
So if we take a look at the code I have here,
this is the code that allows me
to retrieve the current date and time.
So you’ll see here there’s a line of code:
from datetime import datetime.
from datetime import datetime
I said we’re going to talk about libraries a little bit later,
don’t stress out too much about that line of code.
It’s basically a way of saying, “Hey,
there’s a datetime function out there,
that’s pretty cool that someone else wrote for me.
I want to use that, it’s in a library.”
So this line of code allows me
to use that other person’s code in my code.
And then, I have a line here, datetime.now.
So what I’m doing is I’m calling the now function of datetime,
and that will return the current date.
and I’m storing that inside of variable.
So now I have this variable current_date,
which contains the current date and time,
and that I’m doing a print statement.
Let’s actually do this initially.
We’re just saying print the current_date,
just so you can see how that works,
and go down here and we’ll run that code,
get_current_date.py, and, boom, code blows up.
get_current_date.py 然后 嘭 代码炸了
Remember we were talking about data types and type conversions?
Same things will come up when you’re working with dates.
So what happens is,
we see an error that says, I can only concatenate strings,
I don’t know how to concatenate datetimes.
Yeah, I actually don’t know what, you know,
5th of June plus Susan equals,
and neither does Python.
So what we have to do is,
just like when we worked with numbers,
we have to convert the date into a string
because Python does know
how to concatenate a string and a string together.
So by converting that to a string,
now I successfully get a message: Today is…
现在我终于得到了: Today is…
and you can now have the secret,
you now know of Susan is recording this on the 10th of June,
no matter when you’re watching this.
So another thing we can do
once we have a variable stored in datetime object is
that these functions that we can call that will act on the dates,
and the functions are really magical.
So what I’ve done now is I’ve added a second function
that I want to call at the top and below in my code.
So I’m still importing datetime,
but I’m calling another one called timedelta.
That’s a neat little function
that allows me to like, specify a period of time
like, x days, or five days, three days, one week, six weeks, and so on.
如 x天 或者5天 3天 1周 6周 等等
So I start off the same,
I ask for the current date and time,
and I’m putting that into a variable called today,
and I’m displaying on the screen.
So this is more or less the same code we just run,
but here’s the new code.
I say, let’s use timedelta,
and say let’s add one day.
So I say, “Take timedelta, I’d like you to add a period of one day.”
So this is a parameter you pass in,
I know that from looking up the documentation at the timedelta function,
that’s how you learn how to change the values
whether you can pass in weeks, months,
you would have to look at the timedelta function documentation.
But what’s neat is now I can actually say,
yesterday equals today minus one day.
So now I can actually do math on a date value
and then print that out on the screen,
or I could even say down here lower,
let’s move comment this out for now just to keep it simple.
So now, if I run this code,
you can see it says today is, it said nine.
Doing this demo on the 10th of June,
it says yesterday was the ninth of June.
That capability of being able to subtract and add days is really, really powerful.
I once worked on some cool code,
it was a module about 1,500 lines long,
that basically had to convert Julian dates to Gregorian dates
to figure out if it was a leap year and things like that
to basically do this.
So the fact that there’s a function we can call that does it for us,
it’s huge, it’s incredibly useful
if you’re doing anything with dates.
Let’s just go back to the code I want to show you,
it’s not limited to days.
Here, I passed many days equals one,
but I could have just as easily, we comment that out.
I have a line here and I say,
what if we subtract a week?
So when I call the timedelta function,
I ask it to do a Delta of one week,
and then I subtract one week from today.
Well, now if I go and run that code,
we can see it comes back and goes yep one week ago,
if today is the 10th, one week ago was the third of June.
So and it takes care of all that stuff like was
this month 30 days or 31 days,
and is it a leap year,
it’s all taken care of for me.
So timedelta is an example of one of the neat functions you can play with, with dates,
there’s a lot out there.
Suffice it to say,
if you want to do something with date, do a little searching,
you may find a function that does it for you
before you start coding it yourself.
Now, when you displaying a date to somebody,
you probably need to format it.
You may not always want the full date,
hours, minutes, seconds, and milliseconds.
时 分 秒和毫秒
So if we take a look at this code here,
what I’ve done is,
I’m actually extracting just the part of the date that I’m interested in.
I start off again by saying I want to use the datetime function,
in the datetime library, so it’s such a great feature.
I ask for the current date and time by asking for datetime.now,
storing that in a variable called today.
Then what I’m doing is,
if I asked today.day,
that will return just the day portion.
Today.month, the month portion,
year of the year, hour of the hour, and so on.
年份 小时 等等同理
So then, I can take those values
and display them all in the screen.
So I can access whatever part of the date that I want,
you’re not forced to display the full date every single time.
So that way, you can extract the components you need
and just use the parts you’re interested in.
Now, sometimes when you’re working with dates,
you run into some interesting complications.
Let’s say you want to read a value that’s coming out of a file,
or a database, or a user has given you a date.
Unfortunately, most of the time
when you read a date from a file, a database, or from a person,
it will be treated as a string when it’s read by Python.
But if I want to use these cool date functions,
I need it to be treated as a datetime data type.
So once again, I’m going to have to do data type conversions.
Now, I need to convert from string to date,
but how do I know
whether you gave me the date as day-month-year or month-day-year,
and did you use slashes or dashes?
All these are extra complications.
So I have some code here
that will actually take a string and convert it to a date.
What you’ll see is, I ask somebody to enter their birthday.
So I’m using an input statement
and I tell them, “Please give me the date in this format.”
The user may not read the instructions,
but I at least want to remind them the format I’m expecting.
Then what I do is,
I use this strptime function.
So what this does is, it strips elements out of the string
and converts it into a date-time format.
So you say, here’s the string, birthday
that contains the strings someone typed in
and then I have to tell it what format I think the date’s going to be in.
Percent d, day first, then month, percent m, then percent Y,
which means four digit year.
How did I know those values?
I looked up the documentation on the strptime function,
that’s how you learn the syntax for this.
Then I can print out a value on the screen
and I can use that timedelta function to say
what was it the string one day before my birthday and all those great things.
So let’s just try this out and see if it works.
I’m going to go ahead and run my code,
and it says, when is my birthday? Day/month/year,
my birthday is clearly on the sixth of June, 1999.
So I run, it says “your birthday is the 10th of June”,
and the day before your birthday was the fifth of June.
So you can see I know it successfully converted it to a date
because I’m able to use those datetime functions, that timedelta.
But what if the person who is running your code
isn’t paying attention?
They say, “When’s my birthday?”
“It’s the 30th of February.”
or maybe they just enter a valid date,
but they enter dashes instead of slashes.
So they enter something that doesn’t match that format
that I’d said the date would be received in.
Well, boom, here goes the code blowing up all over the place.
然后 嘭 代码又炸了
So this is one of those situations
where you know obviously what’s going to happen occasionally in your code.
So what you should do here is add some exception handling.
So I think maybe we should look at that in the next module.
>> All right. Let’s jump into the code