Python Forum
Printing current time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing current time
#1
I am doing an online course and there is an excercise where I need to print the current time. The question looks like this-

#Complete the code below. We've gone ahead and created
#variables for the current date and current time. Now,
#we want to print date with the format year/month/day,
#and the time with the format hour:minute:second.

from datetime import date
import datetime
todaysDate = date.today()
currentTime = datetime.datetime.now()

#Don't modify the code above!

#Complete the line below to print today's date with the
#form year/month/date. For example, January 15th, 2017
#would be 2016/1/15.
print()

#Complete the line below to print the current time with
#the form hour:minute:second, such as 12:57:15. Don't worry
#about the leading 0s for single-digit times. If it's
#1:05PM and 7 seconds, the correct answer would be:
#13:5:7 (13 because Python uses 24-hour timeby default).
print()

And this is what I did-

#Complete the code below. We've gone ahead and created
#variables for the current date and current time. Now,
#we want to print date with the format year/month/day,
#and the time with the format hour:minute:second.

from datetime import date
import datetime
todaysDate = date.today()
currentTime = datetime.datetime.now()

#Don't modify the code above!

#Complete the line below to print today's date with the
#form year/month/date. For example, January 15th, 2017
#would be 2016/1/15.
print(date.today())

#Complete the line below to print the current time with
#the form hour:minute:second, such as 12:57:15. Don't worry
#about the leading 0s for single-digit times. If it's
#1:05PM and 7 seconds, the correct answer would be:
#13:5:7 (13 because Python uses 24-hour timeby default).
print(currentTime)
But the output is coming as-
2017-04-03
2017-04-03 07:47:48.436353

The date is outputted correctly but why does the time have the date before it and some random numbers after it?
What did I do wrong?

Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#2
The datetime function returns Date then Time. Those random numbers you see, are not random numbers but rather the milliseconds.

I usually use the time formatted to the following. time()


import time
time.strftime("%H:%M:%S")
Reply
#3
OK I got it now. Thank you!
Reply
#4
I would assume you are expected to use the pre-defined variables...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020