Working with times - Answers
Q1. Type this in and get it working:
import datetime
newTime = datetime.datetime.today()
print(newTime)
When you run this code, something similar to this is displayed: 2013-11-13 18:39:55.472872.
Q2. To display the minutes, use %M.
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"))
When you run the code, something similar to this is displayed:
2013-11-13 18:41:18.025944
The time is 18:41
Q4. Using a code from the table, modify the above program so that is displays either AM or PM along with the time.
print(newTime.strftime("The time is %H:%M %p"))
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.
print(newTime.strftime("The time is %I:%M %p"))
Q6. Using a code from the table, modify the above program so that it also displays the seconds.
print(newTime.strftime("The time is %I:%M:%S %p"))
Q7. Using a code from the table, modify the above program so that it also displays the microseconds.
print(newTime.strftime("The time is %I:%M:%S:%f %p"))
Q8. Write a program to neatly display the date and time together. E.g.
import datetime
newTime = datetime.datetime.today()
print(newTime)
print(newTime.strftime("The date is %A %d/%m/%y"))
print(newTime.strftime("The time is %H:%M"))
Q9 - Q10. Research. Aggod article on timezones is here: http://www.bbc.co.uk/news/world-12849630