Show Current Second, Minute & Hour in Python (3 Examples)

In this Python programming tutorial, you’ll learn how to extract the current second, minute and hour.

Creation of Example Data

At the beginning, we have to load the module datetime to Python as you can see here:

import datetime                   # Import datetime module

To make the examples clearer, we create an example datetime object:

date_x = datetime.datetime.now()  # Generating example data
print(date_x)
# 2022-05-15 10:09:01.535866

Example 1: Return Second of Current Time

If we want to print the current seconds of our datetime we can apply the attribute second:

actual_sec = date_x.second        # Using attribute second
print(actual_sec)
# 1

Example 2: Return Minute of Current Time

For extracting the minutes of our current time, we simply use minute as attribute:

actual_min = date_x.minute        # Using attribute minute
print(actual_min) 
# 9

Example 3: Return Hour of Current Time

And to get the number of hours of the current datetime we change the attribute to hour:

actual_h = date_x.hour            # Using attribute hour
print(actual_h) 
# 10

 

Matthias Bäuerlen Python Programmer

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top