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 # [1] "2/05/2020 11:59:22" "12/06/2021 08:22:35" # [3] "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
We can turn our strings into dates and then format our vector by using the format() and as.POSIXct() functions:
date1 <- format(as.POSIXct(date_time, format = '%m/%d/%Y %H:%M:%S'), format = '%m/%d/%Y') date1 # [1] "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:
install.packages('lubridate')
Next, load the package:
library(lubridate)
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 # [1] "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 # [1] "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.
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.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







