>> So we worked with strings,
we worked with numbers,
I think we're ready for dates.
I say that with a heavy sigh because dates
come with extra complications,
and one of the more difficult datatypes to work
with in any programming language.
The most common thing we need when working with date,
is simply I need the current date and time.
We use this a lot when we're logging
errors or saving records and databases,
we want to know when it was saved or when
that record was written or when something happened.
So the way to get the current date and
time is by using the datetime library.
Now, we haven't covered libraries yet,
trust me on this one, we're going to get there.
So for now, don't panic,
just know that I'm using a function, but it is in a library,
this is a very Python type thing to do,
there's a lot of libraries that do cool
stuff that save us a lot of time, they are your friends.
So I'm going to use the datetime library,
and in particular of a datetime function in
the datetime library to ask for current date and time.
So we'll just take a look at that and see how it shows in code.
So the line there from datetime import datetime,
that's basically saying, get me
the datetime function from the datetime library.
More on that later in the libraries module.
Then I can call datetime now,
and that will return the current date and time.
Notice, hey, I'm trying to be good with
comments here on adding comments my code,
so I can remember all of this.
So the Now Function returns a datetime object.
So I can say today
is we were learning about data type conversions.
If I just pass it current date it'll go what?
One's a string, one's a data,
I'm confused. That's okay.
You can just convert your date into a string
just like we can convert numbers into a string,
so we can print the output out on the screen.
So I now have a way of getting
the current date using datetime now,
and if I can convert date to a string datatype,
I can even combine it and displayed on outputs.
There's a whole bunch of functions you can
use once you start playing with datetimes,
that's why they're so wonderful to work with.
So again, I go ask for datetime now.
So I'm storing that in a variable called today,
and then I can display today's date,
so that's the same code I had before.
But you'll actually notice on the top,
I've added another function I want to use is called time delta,
it's in the same library,
it's in that datetime library, but it's cool.
Because it allows me to say
how many days from today or
how many weeks from today something is.
So I can define a time period of one day,
and it's a time delta of days equals one.
If I want a date equals three, it would be three days.
If I wanted to measure a week ago,
I could say weeks equals one.
I simply say, so yesterday equals today minus one day.
Now, think about how complicated that really is
because if today is the first of March,
today minus one day,
well that's 28th of February,
but wait was it a leap year if so,
then it's 29th of February.
I don't want to have to write
programming logic that figures all that out.
So it's much easier for me to use
this time delta function and let the date magic of Python work,
and say just what was the date one day ago?
Then I can just print that out on the screen.
So today was when I printed the slide,
it was the sixth of June,
and then yesterday was the fifth of June.
So time delta needs features really make dates worthwhile,
and save you a lot of time coding.
What it comes down to is when you are working with dates,
if there's something you want to do,
there's probably a function in
datetime or another library that'll do it for you.
If you find yourself trying to count how many days in
a week or months in a year or anything like that,
chances are there's a function that'll
do it for you to save yourself some time.
Now, what if I'm printing a date on the screen?
By default it was displaying a very long date barrows,
day, time, hour, minutes, seconds, milliseconds.
If you want to format it differently,
you could absolutely just request parts of it.
So I can ask for current date.day.
So current have taken datetime now,
the current date and time stored it in
a variable current date, and then I can say,
just give me the day portion,
just give me the month portion,
just give me the year portion,
there's also one for hours, minutes, and seconds.
That way, I can decide what part of a date is important to me when
I'm either saving that data or
using that data or displaying that value.
Now, sometimes somebody won't give you a date,
and then you need to store it as a date.
Okay. Maybe that doesn't make a lot of sense,
but think back to what I was saying in the numbers module,
when I was talking about when you use
the input function and you ask for value,
they'd always get stored as a string.
So I say, hey when's your birthday?
I say my birthday is on the fifth of June 1999,
yeah, we'll go with that.
Then you want to store that value,
because right now it's the string,
you want to store it as a date.
One of the things that's more complicated
is when you take a string,
and you say stored as a date,
you have to know was a date given to you in day, month,
year or as months, day, year,
where you passed in a two-digit year or four digit year,
did they separate the day and month with
slashes or dashes or just spaces.
This is one of the reasons dates are so much fun to work with,
or so challenging to work with.
So if I run this code here,
you'll see I've used the function STRP time.
So this will basically allows me to say,
here's the format I will be receiving.
So I am expecting a date format which is day,
then month, then year.
If you look up the documentation on this function,
it'll tell you-all a little abbreviation to
use for two digit year,
four digit year, month, day etc.
Then, I can take
that string that was received when I use the input function,
convert that into a date function.
Now, that seems like a lot of work if
all I'm doing is displaying the date on the screen.
But remember isn't really cool
functions that come with the date function,
things that allow me to add track days and so on.
That's why it's worth sometimes taking a date value that's stored
in a string and actually converting it into a datetime object.
If all you're doing is storing, it
you can just leave it as a string,
but if you want to play all those cool functions,
you need it to actually be stored as a datetime,
and that's when you didn't need that strip time.
So then I can do things like say,
what was the day before my birthday?
So once I've converted it using that STRP time function,
then I have the ability to
use that time delta function we saw earlier,
and find out what was the date one day
before my birthday, and so on.
Awesome. But as soon as you start doing this in your code,
you're going to want to add some exception handling,
because eventually someone's going to enter a value,
and the format they enter they might enter the 30th of February,
or they might enter month,
day, year instead day, month,
year, and boom, your code's going to blow up.
So let's make sure we handle exceptions gracefully.
If somebody enters the date in
the wrong format rather than
having it just blow up in their faces.
So error handling is also very important,
and we're going to cover that in a later module.