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() function and then convert that character using as.Date() function and finally convert into date using “%Y%m%d” format

Syntax:

 as.Date(as.character(integer),format = “%Y%m%d”)

where, an integer is an input number

Example: Convert Numbers to date

R




# declare an integer
data=2021112
 
# display
print( as.Date(as.character(data),format = "%Y%m%d"))
 
# declare an integer
data1=20201209
 
# display
print( as.Date(as.character(data1),format = "%Y%m%d"))


 Output: 

[1] "2021-11-02"
[1] "2020-12-09"

How to Convert Numbers to Dates in R?

In this article, we will discuss how to convert Numbers to Dates in R programming language.

Similar Reads

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() function and then convert that character using as.Date() function and finally convert into date using “%Y%m%d” format...

Method 2 : Convert Integer to Date Using strptime() Function

...

Method 3: Convert Integer to Date Using ymd() Function of lubridate Package

strptime() is used to convert into the date from an integer in the “%Y%m%d” format...