Working with dates
Working with dates and times in Python can be quite complex if we are not too careful. We are going to use the simple, practical approach!
Q1. Type in the following code and get it working:
import datetime
theDate = datetime.date.today()
print(theDate)
What does it print out?
Q2. There is a lot going on in this simple bit of code! Firstly, we imported a module of useful classes for manipulating dates and times called datetime. Then we created a new object called theDate. We did this by calling the method today(), which is in the class date, and this class is in the module datetime! Don't worry if you are not entirely sure what all this means! The result, however, is that you now have got a lot of information stored in your object called theDate. We can get to this information and use it using various methods. One very useful method is called strftime. 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. |
The code for displaying the actual name of a day (as we will see in a moment) is %A. What is the code for displaying a month's actual name?
Q3. Type this code in and get it working:
import datetime
theDate = datetime.date.today()
print(theDate)
print(theDate.strftime("Today is %A %m/%d/%Y"))
What gets displayed? By using codes from the above table and the method strftime, we can now display the date in any way we want.
Q4. Modify the code above so that the date is displayed in a very similar way to the following.
Wed 13-11-2013
NOTE: The day should be the abbreviated form of whatever day it is when you tackle this problem. Make sure dashes are used instead of slashes in the date and that the date is in this order: days-months-year.
Q5. Modify the code above so that the date is displayed in a very similar way to the following.
Today is Wednesday 13 November 2013
Q6. Modify the code above so that the date is displayed in a very similar way to the following.
Today is Wednesday 05/11/13
Q7. There are 52 weeks in the year. Write some code to display the week number e.g.
The week number is 45.
Q8. Is the first week in the year 0 or 1?
Q9. There are 7 days in a week. Write some code to display the day number e.g.
The day number is 4.
Q10. Is the first day in the week 0 or 1?
Extension task
Have a look around the offical documentation for the datetime module: http://docs.python.org/3.3/library/datetime.html Although it looks very complex, you may find things in there that you can understand and try out!