Open in App
Not now

# How to Convert Numbers to Dates in R?

• Last Updated : 28 Dec, 2021

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() 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"```

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

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

Syntax:

`strptime(integer, format = "%Y%m%d")`

Example: Convert Numbers to date

## R

 `# declare an integer` `data=2021112`   `# display` `print``( ``strptime``(data, format = ``"%Y%m%d"``))`   `# declare an integer` `data1=20201209`   `# display` `print``( ``strptime``(data1, format = ``"%Y%m%d"``))`

Output:

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

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

Here ymd() is used to convert the integer into year, month, and date which is available in lubridate() package

Syntax:

`ymd(integer)`

Example: Convert Numbers to date

## R

 `# load the package` `library``(``"lubridate"``) `   `# declare an integer` `data=2021112`   `# display` `print``(``ymd``(data))`   `# declare an integer` `data1=20201209`   `# display` `print``(``ymd``(data1))`

Output:

```[1] "202-11-12"
[1] "2020-12-09"```

My Personal Notes arrow_drop_up
Related Articles