Why does it display 24-hour time?


Why does it display 24-hour time?


The default format displayed will be 24-hour time, which isn’t what everyone prefers to read. If you want to change this, we’re going to have to use some code we haven’t seen before, but that’s okay! All code is new until we use it and learn it. Take a look at the code below and then I’ll explain how it works:

from datetime import datetime

now = datetime.now()

#24-hour format
print(now.strftime('%Y/%m/%d %H:%M:%S'))
#12-hour format
print(now.strftime('%Y/%m/%d %I:%M:%S'))

We use strftime() to explicitly format the date. The codes we use are Y, m, d, H, M, S, and I. For a full understanding of all the available formatting codes you can pass to strftime(), take a look at the documentation.
In short, we use %I to use a 12-hour clock, and the rest match the first letter of what they format.


Can you explain why some codes are capital letters while others are not?

Directives may sometimes come in two forms, each with their own meaning.

%y  =>  00, 01, 02, 98, 99

%Y  =>  1900, 2000, 2019

A complete table of directives and their meanings is given on the documentation page.


Is this Greenwich Mean Time? The clock is 5 hours ahead of EST.


It seems to output Greenwich Mean Time.
How can I set it to a different time zone so that it displays the right time for my current location?


her we declare now as a variable but is an inbuilt function,how it diffirentiate between these two?