Datetime format


#1

https://www.codecademy.com/courses/learn-python/lessons/date-and-time/exercises/hot-date?action=resume_content_item

I am not able understand that what are those ‘%02d’ and %04d’ instead we can also use '%s
please tell!

Here is another one

https://www.codecademy.com/courses/learn-python/lessons/date-and-time/exercises/getting-the-current-date-and-time?action=resume_content_item

Is we always need to assign a variable to get “month,year,days” and other things?


#2

%d is a placeholder for decimals values (d of decimals what a coincidence) while %s is a string placeholder (s of string, that can’t be a coincidence anymore)

its explained in the lesson:

%02d pads the month and day numbers with zeros to two places, and %04d pads the year to four places. This is one way dates are commonly displayed.

if we just did:

print '%d-%d-%d' % (now.month, now.day, now.year)

then we would get:

19-3-2018

while with %02d we get 03 for march instead of 3 with %d. It pads any shortage with zeroes, i think its quite neat.


#3

Ok got it! Thanks :smile:


#4