Jose: Hi guys and welcome back.
In this video, I wanted to tell you about
the datetime module, which is a built-in module,
part of the Python standard library
that we can use to deal with dates and times
in our Python code.
What is a datetime object?
This is really the main part of the datetime module.
It allows us to store information
about a certain point in time.
Here in the code block on the left
I've imported the daytime module,
and then created a datetime object
which lives inside the datetime module.
So you're gonna see this datetime.datetime stuff
quite often when your working with Python code,
and then inside it you can pass in
your date and time information
with those keyword arguments.
When you print it out, by default,
it prints out the date and time in the ISO format,
but we can look at how you can change that
in just a moment.
You can also compare dates and times
to see which one is greater than another one for example.
With just the usual mathematical operators.
So you can use greater than, less than, and so on.
And we're gonna leave a resource in the resources section
of this lecture with some more information
of what you can do with these datetime objects.
One of the most common things we do with datetime objects
is get the time right now, and this is how you do it.
You use datetime.datetime.now,
and that is gonna give you the current date and time
up to the microsecond, so it's pretty accurate.
This uses your system's local time
to give you the current time,
so it is gonna show you the time that your computer has.
You can also do datetime.datetime.today
to do essentially the same thing,
but it's generally a less precise alternative
because it doesn't have the same support for timezones,
and we're gonna get into what that is
in a couple of videos time,
but I generally recommend using datetime.datetime.now.
Let's take a look at how to display dates
in a different format other than ISO, and how to parse dates
or how to get a string representing a date
and build a datetime object out of them.
Here's how you can display dates.
What we do is we create a date, for example,
datetime.datetime.now,
and then we do the datetime objects method
and then we grab the datetime object, such as today,
and we do strftime, that's string format time.
That's a method in the datetime object,
and what we pass in is a string that uses
the string formatting mini-language.
For example, for the first one there,
%Y means the year has four digits.
And %m means the month has two digits.
%d means the day has two digits.
So that prints out "2019-12-23," for example.
We've got a complete reference for
the string format mini-language over at strftime.org.
We've not written that, but it's really good.
It has everything that you might possible need
when working with these strings.
In order to parse dates, for example,
here we're asking the user for the current date,
and we can assume they entered the day,
then the month, then the year,
you can then use the datetime class strptime
for string parse time,
to grab the string in the first argument
and pass in the formatting string as the second argument
and then the datetime object is going to be
created for you using that information.
Note that because we've got day, month, and year information
in this string, when we parse it using
%d, %m, %Y, what we end up getting is
that datetime object, but without any time information,
so that just defaults to midnight.
If you wanted to put in hours and minutes and so forth
you could use %h, for example, %m,
and that would give you that.
Again, more information at strftime.org.
Let's take a look at what a timestamp is,
and that is the number of seconds that have passed
since the first of January 1970 at midnight.
Why do we use the number of seconds?
Well, simply because it's easier to work with.
It's easier to work with a single number
rather than many parts of the date like
year, day, month, hour, et cetera.
Datetime objects really have a method called timestamp
that allows us to convert the datetime object
that we are using over to a timestamp.
So you can create the datetime object,
for example with .now, and then convert
to a timestamp with .timestamp.
That is gonna give you a float
that has the decimal place there
for the number of microseconds
because everything before the decimal place is just seconds.
That's just some extra precision there
that often we don't use,
but you can use if you want.
Thank you guys for joining me in this video.
I hope you've learned something.
I'll see you in the next one.