R: Use lubridate to Extract Time from Datetime

Often you may want to extract only the time from a datetime object in R.

Fortunately this is easy to do by using the as_hms() function from the hms package in R, which is designed to perform this exact task.

The as_hms() function uses the following basic syntax:



  • x: The name of a datetime object

The following example shows how to use the as_hms() function from the hms package in practice.

Note: Before using the as_hms() function, you may need to first install the hms package by using the following syntax:


Once the hms package is installed, you can use the as_hms() function.

Note: The hms package and the lubridate package are often used together to work with datetimes. The lubridate package has a variety of functions that can handle both dates and times while the hms package can be used specifically to deal with the time portion of datetime objects.

By using these two packages together, we are able to set and retrieve various aspects of datetime objects in R, which we will see int he following example.

Example: Use lubridate to Extract Time from Datetime

Suppose we have the following data frame in R that contains information about sales made at some retail store:

#create data frame
df <- data.frame(dt=as.POSIXct(c('2023-01-01 10:14:00 AM', '2023-01-12 5:58 PM',
                                 '2023-02-23 4:13:22 AM', '2023-02-25 10:19:03 PM')),
                 sales = c(12, 15, 24, 31))

#view data frame

                   dt sales
1 2023-01-01 10:14:00    12
2 2023-01-12 05:58:00    15
3 2023-02-23 04:13:00    24
4 2023-02-25 10:19:00    31

The dt column contains the date and time of the sale while the sales column contains the total number of units sold.

Note that we used the as.POSIXct() function in R to convert each of the strings in the vector to recognizable datetime objects. This ensures that the dt column is interpreted as a datetime class and not as a character class.

Suppose that we would like to extract only the time portion from each datetime in the dt column.

We can use the as_hms() function from the lubridate package to do so:


#add new column that extracts time from dt column
df$time <- hms::as_hms(df$dt)

#view updated data frame

                   dt sales     time
1 2023-01-01 10:14:00    12 10:14:00
2 2023-01-12 05:58:00    15 05:58:00
3 2023-02-23 04:13:00    24 04:13:00
4 2023-02-25 10:19:00    31 10:19:00

We can see that a new column named time has been added to the date frame that contains only the time portion of the corresponding datetime shown in the dt column.

For example, we can see:

  • The time 10:14:00 has been extracted from the first datetime.
  • The time 05:58:00 has been extracted from the second datetime.
  • The time 04:13:00 has been extracted from the third datetime.
  • The time 10:19:00 has been extracted from the fourth datetime.

Note that in this particular example we used the as_hms() function from the hms package in R, which works smoothly with the lubridate package to handle operations on times.


Note: You can find the complete documentation for the as_hms() function in the hms package here.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Generate a Sequence of Dates in R
How to Convert Strings to Dates in R
How to Calculate Number of Months Between Dates in R

Leave a Reply

Your email address will not be published. Required fields are marked *