We live in a large, 4-dimensional place called “spacetime.”
Your location is determined by 3 coordinates, and your place in time is identified by a
single number, T. But here on Earth things are a bit more complicated.
We use a date to determine the day, and a time to specify a point during that day.
Once we colonize our solar system, a better system will be required.
Until then, we must continue to work with dates and times.
Python takes pity on us Earthlings and gives us a variety of classes for working with clocks
and calendars.
So put on your fancy pants, because we have a date with time...
All of the classes we will discuss today live inside the datetime module.
So first things first -- import the datetime module.
Let’s see what is inside this module by printing its directory.
This module contains a date class… a time class… and a datetime class…
The “date” class is for working with days.
The “time” class is for times.
And the “datetime” class is a combination of both the “date” and “time” classes.
It’s a fully-featured class that gives you everything you need to specify a precise moment
in time
Let’s first learn about the “date” class.
It’s always a good idea to look at the help text before using a new class.
We see that Python’s “date” class will support all dates from the year 1 up until
the year 9,999.
So any code we write should last for quite awhile.
I will wager 3 bitcoin that human/computer hybrids will be using a new language in the
year 10,000.
If you scroll to the top of the help text you will see how to create a date object.
To make a date, you pass in 3 numbers: the year, the month and the day.
Let’s make a date called “gvr” with the date January 31, 1956.
This is the birthday of Guido van Rossum, the creator of Python. {{ show photo of Guido
van Rossum }} If you print “gvr”, you can see the date
was created successfully.
And you can access the year…
month… and day separately.
The datetime module contains a class called “timedelta” which lets you add or subtract
a number of days from a date.
For example, let “mill” be January 1, 2000.
A day when people behaved in an odd and superstitious manner.
Let’s assume that it took 100 days for people to return to normal.
What date would that be?
To find out, create a “timedelta” object and pass in the number of days.
A positive number will increase the date, and a negative number will decrease the date.
If you print the sum, you see that April 10, 2000 was 100 days after crazy day.
Did you notice that by default, Python displays dates in year-month-day format.
But you can specify a different format if you’d like.
To do so, you must create a format code.
There are more than 20 different codes for you to choose from when specifying a format
for a date or time.
As an example, suppose we want to display the full name of the day, followed by the
full name of the month, the day number, then the year.
There are two ways you can print a date in this format.
The old way is to call the “s-t-r-f-time” method on a date.
Then you pass in the format string using the available codes.
Here, the codes are %A for the full name of the day…
%B for the full name of the month...
%d for the day of the month, and %Y for the 4-digit year.
Be careful when typing the code.
It is case sensitive.
That is a nice looking date.
A more modern way of doing this is to create a string with the format.
You can enter any text that you like, but when you are ready to enter the date pattern,
start with an open brace followed by a colon.
Now you can print this message.
To do this, call the “format” method and pass in the date, time or datetime object.
That is indeed a well-formatted message.
Let’s now return to the datetime module.
Inside this module are 3 key classes: date, time and datetime.
We will now create objects using all three of these classes.
SpaceX first launched a reused rocket on March 30, 2017 at 22:27 UTC..
We can create a launch date object using the date class.
The 3 arguments are year, month and day.
To create a launch time, we will use the time class.
Here, the 3 arguments are hours, minutes and seconds.
And finally, we can create an object with both the date AND time using the datetime
class.
The first 3 arguments are the year, month and day, and the next 3 arguments are the
hour, minutes and seconds.
Let’s now print all 3 objects to see how they differ from one another.
Print the date… the time… then the datetime...
You can see that the launch date only contains date information…
The launch time only contains time information…
And the launch datetime contains everything.
Just like date objects, a time object gives you access to the individual components.
You can access the hour… the minute… and the second…
And as you might suspect, the launch datetime object gives you access to the date and time
components separately.
You can access the year…
month… day… hour… minute… and second...
Another common problem is to access the current datetime.
The datetime class has a method called “today” for just this purpose.
To get the current datetime in UTC format, type the module “datetime”, the class
“datetime” and then call the method “today.”
If you print the datetime, you will get the exact date and time you ran the code on your
computer.
But what’s this??
After the second, there’s a fractional value.
Did I mention that the datetime class can store times down to the microsecond?
It’s not like me to forget…
You can access the microseconds just like the other datetime components.
That’s some serious precision...
Another common task is to take a string representation of a datetime, and convert it into a datetime
object.
For example, let’s convert the string of the date when a human first walked on the
moon into a datetime object.
To do this, we will use a method on the ‘datetime’ class called ‘s-t-r-p-time’ which is an
abbreviation for “string parse time.”
This method name is unfortunate, since it is used to parse both dates and times.
{{ said with frustration }} I would have called it “parse string” but nobody asked me.
When you call this method, the first argument is the string containing the date.
The second argument is expected format.
We use the same datetime patterns as before.
If you print this object, Python will display the value in a standard datetime format.
But it is, in fact, an object, not a string.
As proof, print the type for this object…
This is, indeed, an instance of the datetime class in the datetime module…
The datetime module contains a class called “timedelta” which can store the difference
between two datetimes.
It can also be used to add or subtract an interval of time to another datetime object.
We at Socratica are working hard to shrink the timedelta between our videos.
How can you help?
By implanting the need to watch our videos into the minds of others!
Subtle persuasion… that’s all I demand...