Convert UNIX Timestamp to Date Object in R - GeeksforGeeks

23 May, 2021
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 <- 2012368256datetime <- 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 timestamptimestamp <- 2012368256  # converting to POSIXct notationposixt <- as.POSIXct(timestamp,                     origin = "1970-01-01")  # converting to readable date # time objectdatetime <- as.Date(posixt)print ("DateTime Notation")print (datetime)
Output:
[1] "DateTime Notation" 
[1] "2033-10-08"
Picked
R-DateTime
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
How to change the order of bars in bar chart in R ?