How to Get Current Hour in Python?

How to Get Current Hour in Python?

Namaste, In this article you will learn How to Get Current Hour in Python. Let’s get started :


Example 1:

from datetime import datetime

today = datetime.now()

print("Current Date and Time :", today)
print("Current Hour :", today.hour)

Output :

Current Date and Time : 2022-07-04 08:06:55.168765
 Current Hour : 8

Example 2:

from datetime import datetime

today = datetime.now()

hour2 = today.strftime("%H")
print("Current Hour(24 Hours) :", hour1)

hour2 = today.strftime("%I")
print("Current Hour(12 Hours) :", hour2)

Output :

Current Hour(24 Hours) : 20
 Current Hour(12 Hours) : 08

Also Read :