R Date and Time Functions
kodingwindow@kw:~$ R
...
> Sys.Date()
[1] "2020-06-18"

> Sys.time()
[1] "2020-06-18 17:41:48 IST"

> as.Date(Sys.Date())
[1] "2020-06-18"

> as.Date('2020/06/18')
[1] "2020-06-18"

> as.Date(Sys.Date(),format='%Y-%m-%d')
[1] "2020-06-18"

> as.Date('June 18, 2020',format='%B %d, %Y')
[1] "2020-06-18"

> as.Date('18JUN20',format='%d%b%y')
[1] "2020-06-18"

> weekdays(Sys.Date())
[1] "Thursday"

> date1 <- Sys.Date() > date2 <- as.Date("2020-06-30") > date1-date2 Time difference of -12 days > date1 [1] "2020-06-18" > date2 [1] "2020-06-30" > date2-date1 Time difference of 12 days > difftime(date2,date1,units="weeks") Time difference of 1.714286 weeks > date2 + 20 [1] "2020-07-20" > difftime(date2+20,date1,units="weeks") Time difference of 4.571429 weeks
> seq(Sys.Date(),by='days',length=12) [1] "2020-06-18" "2020-06-19" "2020-06-20" "2020-06-21" "2020-06-22" [6] "2020-06-23" "2020-06-24" "2020-06-25" "2020-06-26" "2020-06-27" [11] "2020-06-28" "2020-06-29" > seq(Sys.Date(),to=as.Date("2020-12-31"),by='4 weeks') [1] "2020-06-18" "2020-07-16" "2020-08-13" "2020-09-10" "2020-10-08" [6] "2020-11-05" "2020-12-03" "2020-12-31"
Advertisement