Convert UNIX Timestamp to Date Object in R (2 Examples)


In this tutorial, I’ll explain how to convert UNIX epoch time objects to a date object in R programming.

The content of the page looks as follows:

Let’s get started…


Example Data

Consider the example data below:

my_time <- 1412368227                                       # Example timestamp

The previous R code shows our example data – It’s a single timestamp (also called Epoch time, POSIX time, seconds since the Epoch, or UNIX Epoch time) stored in the data object my_time.


Example 1: Converting Timestamp to Date Class

The following syntax shows how to convert a UNIX time object to an object with the Date class. First, we are converting our timestamp to the POSIXct using the as.POSIXct function:

my_time_new1 <- as.POSIXct(my_time, origin = "1970-01-01")  # as.POSIXct function
my_time_new1                                                # Return output
# "2014-10-03 22:30:27 CEST"

As you can see based on the previous output of the RStudio console, our time object changed from a simple and hard to read numeric value to a real date. We can also check the class of our updated data using the class function:

class(my_time_new1)                                         # Check class
# "POSIXct" "POSIXt"

Our new date object has the POSIXct class.

Now, we may convert this POSIXct to the Date class as shown below:

my_time_new2 <- as.Date(my_time_new1)                       # Convert POSIXct to Date
my_time_new2                                                # Return output
# "2014-10-03"

The structure of our data changed again, now showing only the year, month, and day of our date. Let’s check the class of our new data object:

class(my_time_new2)                                         # Check class
# "Date"

As expected: Our final output is a data object with the Date class.


Example 2: Converting Timestamp to Date Using lubridate Package

The following R programming syntax shows an alternative R code compared to Example 1. In this example, we are using the lubridate package to modify our UNIX epoch time. We first need to install and load the lubridate package:

install.packages("lubridate")                               # Install & load lubridate

Now, we can apply the as_datetime(my_time) function to convert our original date variable to a readable format:

as_datetime(my_time)                                        # Apply as_datetime function
# "2014-10-03 20:30:27 UTC"

Whether you want to use the functions of Example 1 or 2 is a matter of taste.


Video, Further Resources & Summary

Would you like to know more about the conversion of UNIX / POSIX time in R? Then you may watch the following video of my YouTube channel. I’m showing the contents of this tutorial in the video:



Besides the video, you may have a look at some of the related articles of this website. I have published numerous articles already:


Summary: You learned on this page how to change UNIX timestamps to the Date class in R programming. In case you have any additional questions, please let me know in the comments section below.


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.

4 Comments. Leave new

  • This doesn’t seem to work with an alphanumeric timestamp like 5FD5A62E. Do you know of any modifications to the code that would work with this?
    Thank you!

  • in the first example you get your reply in CEST and in the second as UTC, does r know your time zone and automatically convert to that? What about daylight savings?

    • Hello Mikael,

      The difference in behavior between as.POSIXct and as_datetime from the lubridate package regarding time zones stems from their default settings and how they handle time zone information.

    • as.POSIXct in Base R: This function converts a date-time object to a POSIXct class. By default, as.POSIXct will use the system’s time zone if a time zone is not explicitly specified. This means that if your system is set to the Central European Summer Time (CEST) zone, as.POSIXct will display times in CEST.
    • as_datetime in lubridate: The as_datetime function from the lubridate package is designed to be more consistent with the ISO 8601 standard, which uses Coordinated Universal Time (UTC) as the base. Therefore, unless specified otherwise, as_datetime will assume UTC as the default time zone. This is why it returns times in UTC.
    • For the default settings, you can always consult the functions’ documentation.

      Regarding daylight savings time, R also handles daylight savings time (DST). When you use a time zone that observes DST, R will automatically adjust the times accordingly, assuming the system’s time zone information is up-to-date.

      I hope it is clearer now.



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.