Working with times
When we looked at dates, we created a date object, which we could do because the datetime module had a class to create these objects called date. There are a number of ways we can access the date and that was just one way. There are also a number of ways we can access the current time. We will look at one of them now. The datatime module has a class called datetime (don't be confused by the fact that it has the same name as the module). We can create an object of type datetime and then access not only date information but also time information as well.
Q1. Type this in and get it working:
import datetime
newTime = datetime.datetime.today()
print(newTime)
What is displayed when you run it?
Q2. We have created an object called newTime. This holds a lot of information abou the current date and time. We can get to this information and use it using various methods. One very useful method is called strftime. We met this method when we looked at dates. This method makes use of various arguments (codes) so that we can get back exactly what information we want and in the form we want it in. Here is a table of the possible arguments that strftime can use.
Code |
Meaning |
%a |
Locale's abbreviated weekday name. |
%A |
Locale's full weekday name. |
%b |
Locale's abbreviated month name. |
%B |
Locale's full month name. |
%c |
Locale's appropriate date and time representation. |
%d |
Day of the month as a decimal number [01,31]. |
%H |
Hour (24-hour clock) as a decimal number [00,23]. |
%I |
Hour (12-hour clock) as a decimal number [01,12]. |
%j |
Day of the year as a decimal number [001,366]. |
%m |
Month as a decimal number [01,12]. |
%M |
Minute as a decimal number [00,59]. |
%p |
Locale's equivalent of either AM or PM. |
%S |
Second as a decimal number [00,61]. |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w |
Weekday as a decimal number [0(Sunday),6]. |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x |
Locale's appropriate date representation. |
%X |
Locale's appropriate time representation. |
%y |
Year without century as a decimal number [00,99]. |
%Y |
Year with century as a decimal number. |
%Z |
Time zone name (no characters if no time zone exists). |
%% |
A literal "%" character. |
We use the code %H to display the hours. What code do we use to display the minutes?
Q3. Add the extra line to the code you have already entered:
import datetime
newTime = datetime.datetime.today()
print(newTime)
print(newTime.strftime("The time is %H:%M"))
What gets displayed now when you run the code?
Q4. Using a code from the table, modify the above program so that is displays either AM or PM along with the time.
Q5. When displaying a time using the 24 hour clock, you don't usually display AM or PM. Change the clock using a code so it uses a 12 hour clock but displays AM or PM.
Q6. Using a code from the table, modify the above program so that it also displays the seconds.
Q7. Using a code from the table, modify the above program so that it also displays the microseconds.
Q8. Write a program to neatly display the date and time together.
Q9. Look at the official documentation for the datetime module. Try out some of the code you find there.
Q10. What is meant by a 'timezone'? Find out what timezone New York is in. How about Delhi? What about Lisbon?