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

 

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

Table of contents:

If you want to know more about these contents, keep reading:

 

Example Data

We first have to load the datetime module:

import datetime                      # Import datetime module

Next, we’ll also have to construct some data that we can use in the examples below:

my_date = datetime.datetime.now()    # Get current datetime
print(my_date)
# 2022-07-05 09:55:34.814728

The previously shown output of the Python console shows the current datetime object as a basis for the next steps.

 

Example 1: Get Current Hour in Python

This example illustrates how to return the current hour of the datetime.

For this, we can use the hour attribute, like you can see here:

current_hour = my_date.hour          # Applying hour attribute of datetime module
print(current_hour)                  # Print hour
# 9

 

Example 2: Get Current Minute in Python

In the next example, I’ll show you how to print the minutes of the current datetime.

current_minute = my_date.minute      # Applying minute attribute of datetime module
print(current_minute)                # Print minute
# 55

 

Example 3: Get Current Second in Python

The last example demonstrates how to extract only the current seconds of our datetime object:

current_second = my_date.second      # Applying second attribute of datetime module
print(current_second)                # Print second
# 34

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. In the video, I explain the Python code of this tutorial:

 

The YouTube video will be added soon.

 

Furthermore, you may have a look at the related articles on this website:

 

In summary: This tutorial has explained how to print the current hour, minute or second in the Python programming language. Don’t hesitate to let me know in the comments section, if you have additional questions.

 

Matthias Bäuerlen Python Programmer

This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top