Get Hour from datetime in Python

In this post, we will see how to get hour from datetime in Python.

Python provides an in-built datetime data type which is utilized by programmers to store date and time. There might be a need to sometimes get several aspects of date and time individually, and this article focuses on one such aspect of the time of the datetime module.

This tutorial demonstrates the different ways available to get hour from datetime in Python. The second half of the article provides a solution on how to get current hour from datetime in Python.

How to get hour from datetime in Python?

Using the time() function and manually slicing out the hour part to get hour from datetime in Python.

To implement this function, we can simply utilize the time() function from the datetime module which is utilized to display the time value only. This time value can then be sliced in order to get just the hour part. The hour part can have a maximum value of 24, hence the slicing is done to get just the first two digits from the given value of the time.

The following code uses the time() function and manually slices out the hour part to get hour from datetime in Python.

The above code provides the following output:

14

Using the % operator along with the datetime.hour attribute to get hour from datetime in Python.

The datetime module provides a datetime.hour attribute that can directly be utilized to represent the hour part of the given time. We can utilize this attribute along with string formatting with the help of the % operator to implement the task at hand.

The vintage method to utilize string formatting in Python, the % operator has always been the go-to way to format strings depending upon the user’s needs in almost all the older versions of Python.

For users that have prior experience with C programming, this method is simple to understand and is still preferred by many programmers to date. The % operator still works in the newer versions of Python, although it is slowly losing popularity due to the introduction of several other methods.

The following code uses the % operator along with the datetime.hour attribute to get hour from datetime in Python.

The above code provides the following output:

The hour value : 14

Using the format() function along with the datetime.hour attribute to get hour from datetime in Python.

The format() function is similar to the % operator and is another way to implement string formatting in Python. The formatted string, after running through the format() function, is provided within the curly braces ({}).

The following code uses the format() function along with the datetime.hour attribute to get hour from datetime in Python.

The above provides the following output:

The hour value : 14

How to get current hour from datetime in Python?

The current hour can also be found and individually represented with the help of several methods, but let us move on to have a look at the best possible method to get current hour from datetime in Python.

Using the datetime.hour attribute to get current hour from datetime in Python.

The datetime.hour can simply take out the value from the current time. The current time will be taken with the help of the datetime.now() function.

The following code uses the datetime.hour attribute to get current hour from datetime in Python.

The above code provides the following output:

current date and time : 2022-03-11 20:02:06.089557
current hour : 20

In the above code, the datetime.now() function stores the current time in the variable x, which can then be accessed through the datetime.hour attribute to get the current hour from datetime in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 18 May

    Convert Month Name to Number in Python

    Table of ContentsHow to convert month name to number in Python?Creating a user-defined function and utilizing it to convert month name to number in Python.Using the calendar module to create a dictionary and utilize it to convert month name to number in Python.Using dict comprehension and the calendar module to convert month name to number […]

  • 18 May

    Add Hours to datetime in Python

    Table of Contentsdatetime in PythonWays to add hours to datetime in PythonUsing the datetime.Hour Attribute to Add Hours to datetime in PythonUsing the datetime.timedelta object to add hours to datetime in PythonUsing the dateutil.Relativedelta Object to Add Hours to datetime in PythonUsing the pandas.Date Offset() Function to Add Hours to datetime in PythonUsing the arrow.shift() […]

  • Format Date to YYYYMMDD in Python
    03 March

    Format Date to YYYYMMDD in Python

    Table of ContentsDate formats in PythonHow to convert date to YYYYMMDD format in PythonUsing datetime.strftime() Function to Format Date to YYYYMMDD in PythonUsing datetime.date Object to Format Date to YYYYMMDD in PythonUsing date.isoformat() Function to Format Date to YYYYMMDD in PythonUsing arrow.format() Function to Format Date to YYYYMMDD in PythonUsing pendulum.to_date_string() Function to Format Date […]

  • Get today's date in Python
    03 March

    Get Today’s Date in Python

    Table of ContentsDate and Time objects in PythonGet Today’s Date in PythonUsing date.today() Function to Get Today’s Date in PythonUsing datetime.today() and datetime.strftime() Functions to Get Today’s Date in PythonUsing datetime.now() Function to Get Today’s Date in PythonUsing strftime() Function to Get Today’s Date in PythonUsing time.strftime() Function to Get Today’s Date in PythonUsing arrow.now() […]

  • Add seconds to datetime in Python
    11 February

    Add Seconds to Datetime in Python

    Table of ContentsDate and Time in PythonWays to add seconds to datetime object in PythonUsing the datetime.timedelta object to add seconds to datetime in PythonUsing the datetime.time.second attribute to add seconds to datetime in PythonUsing the arrow.shift() function to add seconds to arrow object in PythonConclusionWas this post helpful? Date and Time in Python Python […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.