How to subtract number of days from a date to get the previous date in R?


In our daily life, we might want to know what was the date before some number of days. This is also required in professional life, especially in those professions where we work on projects and have tight deadlines. To find the date before a certain number of days we can just use subtraction sign after reading the date with as.Date.

Examples

as.Date("2001-01-01")-30
[1] "2000-12-02"
as.Date("2020-06-30")-30
[1] "2020-05-31"
as.Date("2020-06-30")-50
[1] "2020-05-11"
as.Date("2020-06-30")-100
[1] "2020-03-22"
as.Date("2020-06-30")-120
[1] "2020-03-02"
as.Date("2020-06-30")-15
[1] "2020-06-15"
as.Date("2020-06-30")-45
[1] "2020-05-16"
as.Date("2020-06-30")-40
[1] "2020-05-21"
as.Date("2020-12-25")-20
[1] "2020-12-05"
as.Date("2020-12-25")-300
[1] "2020-02-29"
as.Date("2020-12-25")-125
[1] "2020-08-22"
as.Date("2020-12-25")-80
[1] "2020-10-06"

We can also use / to read the dates as shown below −

as.Date("2020/12/25")-25
[1] "2020-11-30"
as.Date("2020/12/25")-200
[1] "2020-06-08"
as.Date("2020/12/25")-52
[1] "2020-11-03"
as.Date("2020/12/25")-90
[1] "2020-09-26"
as.Date("2020/12/25")-500
[1] "2019-08-13"
as.Date("2020/12/25")-1000
[1] "2018-03-31"

Updated on: 21-Aug-2020

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements