Date and Time

The most recent lab we needed to complete was called ” Date and Time.” In this lab, we were able to learn how to extract different aspects of time without needing to extract all of them, for instance, if you needed to mention the time, you don’t need to mention the year and month.

The point of the exercise was to essentially extract the year, month and day of today by first importing datetime and later merely putting a print statement in front of what you wanted to extract.

Afterwards, we needed to get more specific, this meant we needed to put a separate print statement for year, month, and day.  Depending on what you desired from the time, according to the third part of the exercise, we needed to extract all three, the code is as follows:

from datetime import datetime

now = datetime.now()
print now

print now.year
print now.month
print now.day

 

date-and-time-1

The second part of the exercise taught us how to extract a certain date in order for you to either format it in month/ day/ year or year/ month/ day. Firstly, we needed to import datetime

then print ‘%s/%s/%s’ % (now.month, now.day, now.year)

the %s are essentially place holders for the information in the parenthesis and the / character in between them can easily be changed into a – character depending on your own preference.

You can either present the date like this 2016-11-28 or 11/ 28 /2016.

The code for this part of the exercise is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year)

date-and-time-3

The next part of the exercise demonstrated that in between characters you can also have the : character, and you could get more specific and extract the time now by extracting the hours minutes and seconds. I followed the same steps as before, only this time merely substituting the month, day, and year for hours, minutes, and seconds. The code is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year)
print ‘%s:%s:%s’ % (now.hour, now.minute, now.second)

date-and-time

The last part of the exercise ensured that we knew how to put multiple forms of time within a print statement, meaning more than three different types, which involved a different approach where we needed to add more placeholders, and be more aware of the placement of what we needed to print out. In this case I essentially needed to combine two different lines of code into one print statement by simply adding the placeholders together, and the information within the parenthesis together, but making sure that they were in the exact order in which they needed to be. The code is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year)
print ‘%s:%s:%s’ % (now.hour, now.minute, now.second)
print ‘%s/%s/%s %s:%s:%s’ % (now.month, now.day, now.year,
now.hour, now.minute, now.second)

the-end-of-date-and-time