DATE AND TIME

¡Supera tus tareas y exámenes ahora con Quizwiz!

A lot of times you want to keep track of when something happened. We can do so in Python using datetime. Here we'll use datetime to print the date and time in a nice format.

Click Run to continue. from datetime import datetime

We can use a function called datetime.now() to retrieve the current date and time. from datetime import datetime print datetime.now()

The first line imports the datetime library so that we can use it. The second line will print out the current date and time.

1. 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. Ensure that it has five %02d and one %04d placeholder. 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. 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 placeholders and a ) after them.

from datetime import datetime now = datetime.now() print '%02d/%02d/%04d %02d:%02d:%04d' % (now.month, now.day, now.year, now.hour, now.minute, now.second) 06/11/2020 18:24:0009

Print the current date in the form of mm/dd/yyyy. Change the string used above so that it uses a / character in between the %02d and %04d placeholders instead of a - character.

from datetime import datetime now = datetime.now() print '%02d/%02d/%04d' % (now.month, now.day, now.year) 06/10/2020

Similar to the last exercise, print the current time in the pretty form of hh:mm:ss. Change the string that you are printing so that you have a : character in between the %02d placeholders. Change the three things that you are printing from month, day, and year to now.hour, now.minute, and now.second.

from datetime import datetime now = datetime.now() print '%02d:%02d:%04d' % (now.hour, now.minute, now.second) 05:50:0029

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

from datetime import datetime now = datetime.now() print now 2020-06-08 07:01:21.467339

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

from datetime import datetime now = datetime.now() print now print now.year print now.month print now.day 2020-06-09 05:14:22.581749 2020 6 9


Conjuntos de estudio relacionados

Management of Patients With Musculoskeletal Disorders

View Set

Angle Relationships Instruction/ Assignment/ Quiz

View Set

Web Development and Security IS245

View Set

Adult and Aging Development Midterm 1

View Set

CH 5 - Lesson 3 (New American Diplomacy)

View Set

American History Final (chapters 12-15)

View Set