Python Get Current Hour Example

Published On: 30/08/2022 | Category: Python


Hi Guys,

Now, let's see tutorial of python get current hour example. you'll learn how to get current hour in python. We will use how to get current hour in python 3. This article goes in detailed on python get today hour.

In this post, i will give you three examples to getting current hour in python. we will use datetime module to get current hour from date. so let's see following examples with output:

So let's see bellow example:

Example 1: main.py
from datetime import datetime
  
today = datetime.now()
  
print("Current Date and Time :", today)
print("Current Hour :", today.hour)
Output:
Current Date and Time : 2022-08-30 05:22:56.955981
Current Hour : 5
Example 2: main.py
from datetime import datetime
  
today = datetime.now()
    
hour1 = today.strftime("%H")
print("Current Hour(24 Hours):", hour1);
  
hour2 = today.strftime("%I")
print("Current Hour(12 Hours):", hour2);
Output:
Current Hour(24 Hours): 05
Current Hour(12 Hours): 05

It will help you....