Convert Date to Numeric Time Object in R (Example)


In this tutorial, I’ll explain how to transform a date to a time object in R programming. Furthermore, I’ll show how to extract the hours, minutes, and seconds of this date object.

Table of contents:

Let’s dive right into the examples!


Creation of Example Data

Let’s first create some data that we can use in the following example:

x <- strptime("10:45:15", format = "%H:%M:%S")           # Create date object
x                                                        # Print date object
# "2019-08-13 10:45:15 CEST"

Our example data is a simple date object (i.e. the 13th of August 2019, 10:45:15 am).

Now, let’s convert this date object to a numeric object representing the hours, minutes, and seconds of our date…


Example: Convert Date to Time in R & Extract Time Only

For this task, we need to install and load the lubridate R add-on package:

install.packages("lubridate")                            # Install lubridate package
library("lubridate")                                     # Load lubridate package

The lubridate package contains functions to extract the hours…

hour(x)                                                  # Basic application of hour function
# 10


minute(x)                                                # Basic application of minute function
# 45

…or seconds of a date object:

second(x)                                                # Basic application of second function
# 15

In case we want to convert our date object to a numeric time value, we can use the following code for a conversion to hours…

x_hours <- hour(x) + minute(x) / 60 + second(x) / 3600   # Convert date object to hours
x_hours                                                  # Print hours to RStudio console
# 10.75417

…the following R code for a transformation to minutes…

x_minutes <- hour(x) * 60 + minute(x) + second(x) / 60   # Convert date object to minutes
x_minutes                                                # Print minutes to RStudio console
# 645.25

…and the following R syntax for a conversion to seconds:

x_seconds <- hour(x) * 3600 + minute(x) * 60 + second(x) # Convert date object to seconds
x_seconds                                                # Print seconds to RStudio console
# 38715

Easy going!


Video, Further Resources & Summary

Would you like to know more about the conversion of dates to time in R? Then I can recommend to watch the following video of my YouTube channel. I explain the R programming codes of this article in the video:



Furthermore, you could read the related tutorials which I have published on my homepage:


In this tutorial, I explained how to change the format of a date object in order to extract the time in R programming. In case you have additional questions, don’t hesitate to let me know in the comments section. Furthermore, please subscribe to my email newsletter to get updates on new tutorials.


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. Required fields are marked *

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