python datetime module print 12 hour clock 1

python datetime module print 12 hour clock

from datetime import datetime

now = datetime.now()

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

Here is what the above code is Doing:
1. We import the datetime module.
2. We create a variable called now and set it equal to datetime.now().
3. We print the current date and time, nicely formatted.
4. We print the current date and time in 12-hour format.

Similar Posts