Back

Working with dates - Answers

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)

it prints out 2013-11-13 (or whatever the date is when you run the code).
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. The code for displaying a month's actual name is %B.
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"))

This gets displayed:

2013-11-13
Today is Wednesday 11/13/2013By using codes from the above table and the method strftime, we can now display the date in any way we want.

Q4. To modify the code above so that the date is displayed in a very similar way to the following.

Today is Wed 13-11-2013

do this:

print(theDate.strftime("Today is %a %d-%m-%Y"))

Q5. To modify the code above so that the date is displayed in a very similar way to the following:

Today is Wednesday 13 November 2013

do this:

print(theDate.strftime("Today is %A %d %B %Y"))

Q6. To modify the code above so that the date is displayed in a very similar way to the following.

Today is Wednesday 05/11/13

do this:

print(theDate.strftime("Today is %A %d/%m/%y"))

Q7. There are 52 weeks in the year. To write some code to display the week number e.g.

The week number is 45.

do this:

print(theDate.strftime("The week number is %U."))

Q8. The first week in the year is 0.

Q9. There are 7 days in a week. To write some code to display the day number e.g.

The day number is 4.

do this:

print(theDate.strftime("The day number is %w."))

Q10. The first day in the week is 0.

Back