How to Extract Time from Datetime in R – with Examples

In this short R tutorial, you will learn how to extract time from datetime in R by examples. This task may need to be undertaken whenever you have a dataset and you want to do a time series analysis but only want to include time, for example.

There are many ways we can get this information from different objects in R.  For example, we can use the format function after we have converted a vector to POSIXct class (e.g., by using the as.POSIXct function). Furthermore, we can install packages such as lubridate. In this post, we will go through a couple of examples of extracting time from datetime stored in a character vector and a dataframe. 

Table of Contents


Now, to be able to follow this post, you will need to have R and the readr package installed. If you are going to work with lubridate, you will also need to install this package. Note, to follow this tutorial, you don’t need to install lubridate but if you, later, need to extract year from date, lubridate is a nice r-package to use.

Installing packags to work with
  • Save
Installing only lubridate and readr

Installing lubridate is pretty simple, open up R and type install.packages(‘lubridate’) (exchange lubridate for readr, or see gif above, to install both of them). As lubridate and readr are part of the tidyverse, you can also install them, among other useful packages, by typing install.packages(‘tidyverse’). A lot of these packages are very useful if you, for instance, want to drop a column from the dataframe in R. Furthermore, when calculating descriptive statistics the tidyverse packages are also very useful. 

How to Extract Time from Datetime in R?

Extracting time from datetime is straightforward in R. You can use 1) the format() function: format(YourDates, format = “%H:%M:%S”), and 2) the as.POSIXctfunction: as.POSIXct(dates, format = “%m/%d/%Y %H:%M:%S”).

Example 1: How to Extract Time from a Vector Containing Dates

In the first example, we will get time from a vector (c()) containing dates. Here we will start by converting the characters to dates using the as.POSIXct function together with format. This will make it easier to extract time, day, or year from a character vector. 

Here’s the syntax for extracting time from a vector containing dates and times:

format(YourDates, format = "%H:%M:%S")Code language: R (r)
r extract time from date
  • Save

Of course, YourDates should be a vector containing dates and you will see how you convert a vector so that you can use format to extract the hour, minutes, and seconds.  Some other R tutorials you may find useful:

Converting a Character Vector

First, if we have obtained data and stored it in a character vector (c()), we need to convert it so that we can use format. Thus, in this subsection we will create an example vector and use as.POSIXct().

dates <- c("01/02/2014 11:40:00", "01/03/2015 12:40:00", "01/02/2016 07:40:00", 
           "01/04/2017 09:40:00" , "01/02/2018 01:40:00", "01/12/2019 03:40:00")
dates <- as.POSIXct(dates, format = "%m/%d/%Y %H:%M:%S")
datesCode language: R (r)
separate time from date r
  • Save

Now, we used the vector as an input and as, you may have noticed, the argument format. For clarity, the different letters, of course, reflect how the year (%Y), month (%m), and day (%d) is ordered in your vector. Therefore, you must change this to suit how your datetime data is stored. Finally, “%H:%M:%S”, represents hour, minute, and second. 

Extract Time

We are now ready to use format() to split the timestamp from the datetime. Here’s how to extract hours, minutes, and seconds:

time <- format(dates, format = "%H:%M:%S")Code language: R (r)
extracted time
  • Save
time extracted from date

As previously mentioned, the format of the date, and time, can be changed around by switching the letters. If we only want an hour, for instance, we can make use of this and do like this:

format(dates, format = "%H")Code language: R (r)

It is, of course, possible to use POSIXct() direct and here’s a working code chunk for extracting time:

dates <- as.POSIXct(c("01/02/2014 11:40:00", "01/03/2015 12:40:00", "01/02/2016 07:40:00", 
           "01/04/2017 09:40:00" , "01/02/2018 01:40:00", "01/12/2019 03:40:00"), 
           format = "%m/%d/%Y %H:%M:%S")

format(dates, format = "%H:%M:%S")Code language: R (r)

That was how to get the time from a vector containing datetimes. In the next example, we are going to work with a dataframe. Mostly, when working with real data, we read our data from a file. Therefore, the next section will cover how to read a file (.csv) and extract time from the column containing date and timestamps. 

  • Save

Example 2: How to Extract Time from a Column in a Dataframe

In this example, we are going to read a .csv file, and then we are going to separate time from datetime, and store it in a new column. Here’s how to extract timestamps from a column and add them to a new column:

GET(';timezone=Europe/Stockholm&amp;lang=en&amp;use_labels_for_header=true&amp;csv_separator=%3B', write_disk(tf <-  tempfile(fileext = ".csv")))
df <- read_delim(tf, delim=';')

df$HMS <- format(df$Time, format="%H:%M:%S")
head(df[4:length(names(df))])Code language: R (r)

We used GET() to download the .csv file to a temporary folder in the code chunk above. The path to this temporary file is then used (tf) when we read the file using read_delim(). Finally, we add a new column to the dataframe (named “HMS”) and use format to separate time from the date again. Note it is, of course, possible that the data is stored in an Excel file. Neatly, tidyverse comes with a package, as well, for reading .xls and .xlsx files in R.

dataframe with new column with extracted time from date in r
  • Save

Note that this specific .csv file is delimited by a semicolon. If you have a CSV file separated by commas, you can use the read_csv() function, from the readr package. If your data is stored in other file formats, one of the following posts might help you:

Now that you have your dataframe fixed, and timestamps are extracted to a new column, you may want to calculate desriptives statistics. It is, of course, also possible to extract day from datetime and add to a column in the dataframe.

It is, of course, possible to use other functions and/or r-packages to extract time from datetime in R. In the next examples, we will look at how to use lubridate and the function dmy_hms() instead of POSIXct(). 

Example 3: How to Separate Time from datetime in a Vector with Lubridate

Here’s how to split time from datetime (vector) with using dmy_hms() and format():

dates <- dmy_hms(c("01/01/2014 11:40:00", "01/01/2015 12:40:00", "01/01/2016 07:40:00", 
           "01/01/2017 09:40:00" , "01/01/2018 01:40:00", "01/01/2019 03:40:00"))

dates <- format(dates, format = "%H:%M:%S")Code language: R (r)

Now, we could also create a dataframe, splitting time from datetime, if we want to:

dates <- dmy_hms(c("01/01/2014 11:40:00", "01/01/2015 12:40:00", "01/01/2016 07:40:00", 
           "01/01/2017 09:40:00" , "01/01/2018 01:40:00", "01/01/2019 03:40:00"))

df_dates <- data.frame(date = format(dates, format = "%Y-%m:%d") , time = format(dates, format = "%H:%M:%S"))

head(df_dates)Code language: R (r)

Example 3: Extracting time using as_hms()

In this section, we will look at the as_hms() function from the hms package. Why? Well, as pointed out on an earlier version of this post: format will coerce the POSIXct datetime to a character. Using the as_hms() function, on the other hand, will extract time, into a time object.


In this short R tutorial, you have learned how to extract time from datetime using as.POSIXct(), format, and lubridate (i.e., dmy_hms()). First, you learned how to convert a vector containing characters of datetime to a POSIXct class so you could use format for the extraction. Second, you have also learned to import data, take a column containing datetime, split the time, and add it to a new column.


Here are some very good resources you will find helpful:

  • Save

2 thoughts on “How to Extract Time from Datetime in R – with Examples”

  1. Hi Erik, this post is useful. However, you should stress that using format() coerces your POSIXct “datetime” to a “character”. Thus, for printing/labelling this might not harm. Nonetheless, working with times or doing time series analyses typically includes also doing math with dates/times. This is when the character type will haunt you.
    You mention lubridate for the type initialisation, i.e. using lubridate::dmy_hms() to set up your dates vector. The lubridate package provides for lubridate::date() function to extract the date part of a datetime object. In analogy the hms package provides for hms::as_hms() to coerce the input dates to time objects.

    1. Hey Rainer,

      Thank you for your comment. This is, of course, valuable information for me and people who might find this post. I will update the post according to your suggestions. again, thanks for your comment. Much appriecated,



Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Share via
Copy link
Powered by Social Snap