Datetime.now is cutting off the zero's from the hour/seconds


#1

https://www.codecademy.com/courses/learn-python/lessons/date-and-time/exercises/grand-finale?action=lesson_resume&link_content_target=interstitial_undefined

So my current time when I run datetime is 00:41:07.xxxx
When I try to print the exercise, I’m getting the following:

0:41:7 instead of 00:41:07.

I guess it’s just cutting off those zeroes since 07 is the same as 7 in normal number land, but not with time. How do I get those zeroes back?

I tried to turn now.hour and now.second into strings and print those instead, but that didn’t work.
ie.



second = str(now.second)
hour = str(now.hour)
print "%s:%s:%s" % (hour, now.minute, second) 



#2

Hi @kyrafetrow,

Try this for a better format, padded with leading zeroes where needed …

print '%4d/%02d/%02d %02d:%02d:%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.