ItSolutionStuff.com

How to Get Current Hour in Python?

By Hardik Savani • October 30, 2023
Python

Hello,

This simple article demonstrates of python get current hour. you will learn how to get current hour in python. This post will give you simple example of how to get current hour in python 3. if you have question about python get today hour then I will give simple example with solution. So, let's follow few step to create example of python get current 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:

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-06-02 09:39:52.287195
Current Hour : 9

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): 13
Current Hour(12 Hours): 01

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Current Month in Python?

Read Now →

How to Get Current Day in Python?

Read Now →

How to Get Current Year in Python?

Read Now →

How to Get Current Date and Time in Python?

Read Now →

How to Get Current Date in Python?

Read Now →

Laravel Carbon addYears() | Laravel Carbon Add Year Example

Read Now →

Laravel Carbon Subtract Months from Date Example

Read Now →

Laravel Carbon addMonths() | Laravel Carbon Add Months Example

Read Now →

Laravel Carbon Subtract Days to Date Example

Read Now →