# How to properly convert, achieve the equivalence of dates

Do these work for you?

``````library(data.table)
dt<-as.data.table(list(
yr = c(14610, 14975, 15340, 15706, 16071, 16436, 16801, 17167,
17532, 17897, 18262, 18628, 18993, 19358, 19723, 20089,
20454, 20819, 21184, 21550, 21915, 22280, 22645, 23011,
23376, 23741, 24106, 24472, 24837, 25202, 25567, 25933,
26298, 26663, 27028, 27394, 27759,  28124, 28489, 28855)))
dt[,`:=`(expected=as.IDate(yr),date=as.Date(yr,origin = "1970-01-01")),]
print(dt,topn=3)
#>        yr   expected       date
#>  1: 14610 2010-01-01 2010-01-01
#>  2: 14975 2011-01-01 2011-01-01
#>  3: 15340 2012-01-01 2012-01-01
#> ---
#> 38: 28124 2047-01-01 2047-01-01
#> 39: 28489 2048-01-01 2048-01-01
#> 40: 28855 2049-01-01 2049-01-01

dt2<-as.data.table(list(
yr = c(20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,20200327,
20200327,20200327,20200327)))
dt2[,`:=`(expected=lubridate::ymd(yr),idate=as.IDate(as.character(yr),format="%Y%m%d"),
date=as.Date(as.character(yr),format="%Y%m%d")),]
print(dt2,topn=3)
#>           yr   expected      idate       date
#>  1: 20200327 2020-03-27 2020-03-27 2020-03-27
#>  2: 20200327 2020-03-27 2020-03-27 2020-03-27
#>  3: 20200327 2020-03-27 2020-03-27 2020-03-27
#> ---
#> 19: 20200327 2020-03-27 2020-03-27 2020-03-27
#> 20: 20200327 2020-03-27 2020-03-27 2020-03-27
#> 21: 20200327 2020-03-27 2020-03-27 2020-03-27
``````

Created on 2020-04-03 by the reprex package (v0.3.0)