Namaste, In this article you will learn How to Get Current Hour in Python. Let’s get started :
Table of Contents
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 :
- How to Check if Today is Friday or not in Python?
- How to Get Current Month in Python?
- Python GET Request with Parameters
- Python POST Request with Parameters Example
- Python Get Day Name from Number Example
- Angular Material Grid Layout Example
- Angular Material Card Example