Convert Year, Month, Day, Hour, Minutes & Seconds to Date & Time in R (3 Examples)
In this tutorial you’ll learn how to convert a character string to a date-time object in R programming.
Table of contents:
Let’s do this.
Creation of Example Data
We’ll use the following data as basement for this R programming tutorial:
my_date <- "2023-10-07 11:38:55" # Create example date as character string my_date # Print example date #  "2023-10-07 11:38:55"
Have a look at the previous output of the RStudio console. It shows that our example data is a character string containing a single date and time.
However, the data object is not properly formatted as a date yet. Let’s change this!
Example 1: Convert Character String to Date Class Using as.Date() Function
This example illustrates how to convert a character string to the Date class using the as.Date function.
Have a look at the following R code:
my_date_1 <- as.Date(my_date) # Convert to Date class my_date_1 # Print updated date #  "2023-10-07"
As you can see, we have created a new date object called my_date_1.
However, you can also see that the hours, minutes, and seconds have been removed from our date. This is because the as.Date function only yields dates, but no times.
Next, I’ll explain how to create a data object that contains both dates and times.
Example 2: Convert Character String to POSIXlt Class Using as.POSIXlt() Function
The following R syntax explains how to convert a character string to a data object holding both dates and times.
For this task, we can apply the as.POSIXlt function as shown below:
my_date_2 <- as.POSIXlt(my_date) # Convert to POSIXlt class my_date_2 # Print updated date #  "2023-10-07 11:38:55 UTC"
As you can see, the previous R code has not only converted our character string to a date-time object, it has also added the time zone (i.e. UTC) to the output.
Note that the data type of this date-time object is the POSIXlt class. This is an additional difference compared to as as.Date function, which has created a data object with the Date class.
So far, so good. However, the previous examples have been easy, since our character strings where already formatted properly. Let’s do a more complicated example!
Example 3: Convert Unformatted Character String to POSIXlt Class Using strptime() Function
Example 3 shows how to convert date character strings with a non-standard format to a date-time object with year, month, day, hour, minute, and second.
For this, we first have to create another example character string:
my_date_unformatted <- "10-07-2023 11:38:55" # Create date with different format my_date_unformatted # Print date with different format #  "10-07-2023 11:38:55"
Our new character string contains the same date as the previously used example string. However, this time the date is formatted as month-day-year instead of year-month-day.
For that reason, we have to use the strptime to change our character string to a date-time object. Within the strptime function, we have to specify the ordering of years, months, days, hours, minutes, and seconds within our character string.
Consider the following R syntax:
my_date_3 <- strptime(my_date_unformatted, # Convert to POSIXlt class format = "%m-%d-%Y %H:%M:%S") my_date_3 # Print updated date #  "2023-10-07 11:38:55 UTC"
As you can see, the output of the previous R code is exactly the same as in Example 2, even though the input character string was formatted the wrong way.
The data object that we have created in Example 3 does also have the POSIXlt class.
Video & Further Resources
I have recently released a video on my YouTube channel, which explains the contents of this tutorial. Please find the video below.
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.
In addition to the video, you may have a look at some of the other articles on this website. A selection of tutorials is shown below.
- Check if Column is Date in R
- Get Difference Between Dates in Years
- Convert Integer to Date in R
- Convert Date to Day of Week in R
- Extract Hours, Minutes & Seconds from Date & Time Object
- Introduction to R
Summary: At this point you should know how to convert a character string to a date-time object with hours, minutes, and seconds in R. If you have further questions, tell me about it in the comments section below.
Statistics Globe Newsletter