Open In App

Convert UNIX Timestamp to Date Object in R

Improve
Improve
Like Article
Like
Save
Share
Report

UNIX timestamp refers to the number of seconds that have elapsed since the epoch. The timestamp object is not easily understandable and should be converted to other user-friendly formats. The Date objects in R Programming Language can be used to display the specified timestamp in a crisp way. Date objects are stored as the number of days since January 1, 1970, where the negative numbers are used for earlier dates. Here we will see how to convert UNIX Timestamp to Date Object in R Programming.

Method 1: Using lubridate package

Lubridate package in R is responsible to make it easier to work with dates and times. It contains specialized parsing functions to manipulate and modify time stamps into various different formats and time zones available. The package needs to be installed into the R library using the following syntax : 

install.packages("lubridate")

as_datetime() method in this package is used to convert UNIX Timestamp to Date Object. This method used the UTC timezone by default. 

Syntax: as_datetime(timestamp, tz)    

Arguments : tz – The corresponding time zone

Code:

R




library("lubridate")
timestamp <- 2012368256
datetime <- as_datetime(timestamp)   
  
print ("DateTime Notation")
print (datetime)


Output:

[1] "DateTime Notation" 
[1] "2033-10-08 07:10:56 UTC"

Method 2: Using as.POSIXct method

Timestamp can be first converted to a POSIXct object and then conversions can be carried out. POSIXct objects ease the process of mathematical operations since they rely on seconds as the major unit of time management. The dates are converted to the standard time zone, UTC. A timestamp object can be converted to a POSIXct object, using them as.POSIXct(date) method in R. 

as.POSIXct(timestamp, origin = "1970-01-01")

This is followed by the application of as.Date method over the POSIXct object. The date objects are stored as the number of days calculated starting January 1, 1970, where negative numbers are used to refer to earlier dates. The Date objects support basic arithmetic directly, where in the integers are added or subtracted directly from the Dates. The Date object can also specify different formats to contain the dates. The as.Date() method takes as input a POSIXct date object and converts it to a Date object.

as.Date(character date object)

The difference that this method holds is that it just displays the date object, while the above method converts it to a complete DateTime object. 

Code:

R




# declaring the timestamp
timestamp <- 2012368256
  
# converting to POSIXct notation
posixt <- as.POSIXct(timestamp,
                     origin = "1970-01-01")
  
# converting to readable date 
# time object
datetime <- as.Date(posixt)
print ("DateTime Notation")
print (datetime)


Output:

[1] "DateTime Notation" 
[1] "2033-10-08"


Last Updated : 23 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads