Convert UNIX Timestamp to Date Object in R
Last Updated :
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 <- 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"
Similar Reads
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read
Convert timestamp to readable date/time in PHP Problem: Convert timestamp to readable date/time in PHP Solution: This can be achieved with the help of date() function, which is an inbuilt function in PHP can be used to format the timestamp given by time() function. This function returns a string formatted according to the given format string usi
1 min read
How to Convert Timestamp to Datetime in MySQL? In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
2 min read
How to Convert a UNIX Timestamp (Seconds Since Epoch) to Ruby DateTime? Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially i
4 min read
How to Convert Date to Numeric in R? In this article, we will discuss how to convert date to numeric in R Programming Language. Method 1: Using as.numeric() This function is used to convert date into numeric Syntax: as.numeric(date) where the date is the input date. Example: R data = as.POSIXct("1/1/2021 1:05:00 AM", format="%m/%d/%Y %
2 min read
How to Convert Numbers to Dates in R? In this article, we will discuss how to convert Numbers to Dates in R programming language. Method 1: Convert Integer to Date Using as.Date() & as.character() Functions Here we have to consider an integer and then first we have to convert that integer into a character using as.character() functi
2 min read