Lab Assignment #5

Date and Time


Description
In this assignment, I will add the date and time on the output window in the upper right-hand side of the screen. It’s strange that the time displayed is 4 hours ahead of the Eastern Standard Time. My guess is that the time shown is the time somewhere in Europe. Below are the instructions for viewing the date and time.


The Datetime Library
Print from datetime import datetime

1 datetime


Getting the Current Date and Time

  1. Create a variable called now and store the result of datetime.now() in it.
  2. Then, print the value of now.

2 current date and time


Extracting Information

  1. On a new line, print now.year. Make sure you do it after setting the now variable!
  2. Then, print out now.month.
  3. Finally, print out now.day.

3 extracting info


Hot Date
Print the current date in the form of mm/dd/yyyy.

  1. Change the string so that it uses a / character in between the %s  placeholders instead of a  character.
  2. Re-arrange the parameters to the right of the % operator so that you print now.month, then now.day, then now.year.

4 hot date


Pretty Time
Similar to the last exercise, print the current time in the pretty form of hh:mm:ss.

  1. Change the string that you are printing so that you have a : character in between the %s placeholders.
  2. Change the three things that you are printing from month, day, and year to now.hour, now.minute, and now.second.

5 pretty time


Grand Finale
Print the date and time together in the form: mm/dd/yyyy hh:mm:ss.
To start, change the format string to the left of the % operator.

  1. Ensure that it has 6 %s placeholders.
  2. Put slashes and colons and a space between the placeholders so that they fit the format above. Then, change the variables in the parentheses to the right of the % operator.
  3. Place the variables so that now.month, now.day, now.year are before now.hour, now.minute, now.second. Make sure that there is a ( before the six and a ) after them.

6 grand

Code
from datetime import datetime
now = datetime.now()
print ‘%s/%s/%s %s:%s:%s’ % (now.month, now.day, now.year, now.hour, now.minute, now.second)