Remove Time Component from Date-Time in R (3 Examples)
In this tutorial you’ll learn how to drop the time component from a date-time in R.
The table of content of this tutorial is structured as follows:
Let’s start right away.
Creation of Sample Data
First of all, we will need to create some sample data as a basis for this tutorial. We will create a vector with dates and times:
date_time <- c("2/05/2020 11:59:22", "12/06/2021 08:22:35", "3/10/2022 13:12:03", "7/04/2019 05:11:27") date_time #  "2/05/2020 11:59:22" "12/06/2021 08:22:35" #  "3/10/2022 13:12:03" "7/04/2019 05:11:27"
Example 1: Remove Time Component from Date-Time Using format() & as.POSIXct() Functions
date1 <- format(as.POSIXct(date_time, format = '%m/%d/%Y %H:%M:%S'), format = '%m/%d/%Y') date1 #  "02/05/2020" "12/06/2021" "03/10/2022" "07/04/2019"
Example 2: Remove Time Component from Date-Time Using format() & dmy_hms() Functions
We can also use the format function in combination with the lubridate package to separate time from a date-time in a vector.
For this, please install the lubridate package if you haven’t installed it before:
Next, load the package:
Now, we can use the dmy_hms() function in the lubridate package in combination with the format function to remove the time component from our vector:
date2 <- format(mdy_hms(date_time), format = "%m/%d/%Y") date2 #  "02/05/2020" "12/06/2021" "03/10/2022" "07/04/2019"
Example 3: Remove Time Component from Date-Time Using round() & dmy_hms() Functions
Another alternative is provided by the round function. It can be used to round date-time objects by days:
date3 <- round(mdy_hms(date_time), unit = "day") date3 #  "2020-02-05 UTC" "2021-12-06 UTC" "2022-03-11 UTC" "2019-07-04 UTC"
Note that the previous output still contains the time zone component.
As shown, we can remove the time component from our date-time vector in multiple ways.
Video, Further Resources & Summary
Do you need more explanations on how to extract the date component from a date and time object in R? Then you might have a look at the following video of the Statistics Globe YouTube channel.
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.
Additionally, you could have a look at some of the other tutorials on Statistics Globe:
- Convert Character String with AM & PM to Date & Time in R
- Remove Time from datetime in Python (Example)
- Remove NA Columns from xts Time Series in R
- Convert Four Digit Year Values to Class Date in R
This page has shown how to remove a time component from a date-time object in R. In case you have further questions, you might leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter