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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the related articles on this website:
- Get Current Time in Specific Timezone in Python
- Get Current Day of Week in Python – Number & Name
- Get Current Date & Time in Python
- Get Current Week Number in Python
- All Python Programming Examples
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.
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.