R在日期上增加天数[重复]

时间:2022-08-25 17:43:56

This question already has an answer here:

这个问题已经有了答案:

I have a date that I would like to add days to in order to find some future date.

我有一个日期,我想加上几天,以便找到一些未来的日期。

for instance how would I find the date that is 45 days after 1/1/2001?

例如,我如何找到在2001年1月1日之后45天的日期?

4 个解决方案

#1


61  

Use +

使用+

> as.Date("2001-01-01") + 45
[1] "2001-02-15"

#2


21  

You could also use

您还可以使用

library(lubridate)
dmy("1/1/2001") + days(45)

#3


9  

In addition to the simple addition shown by others, you can also use seq.Date or seq.POSIXt to find other increments or decrements (the POSIXt version does seconds, minutes, hours, etc.):

除了其他人展示的简单添加之外,您还可以使用seq。日期或seq。查找其他增量或递减(POSIXt版本执行秒、分钟、小时等):

> seq.Date( Sys.Date(), length=2, by='3 months' )[2]
[1] "2012-07-25"

#4


9  

Just use

只使用

 as.Date("2001-01-01") + 45

from base R, or date functionality in one of the many contributed packages. My RcppBDT package wraps functionality from Boost Date_Time including things like 'date of third Wednesday' in a given month.

从基本的R,或日期功能,在其中一个许多贡献包。我的RcppBDT包包含Boost Date_Time的功能,包括给定月份的“第三个星期三”。

Edit: And egged on by @Andrie, here is a bit more from RcppBDT (which is mostly a test case for Rcpp modules, really).

编辑:@Andrie推动下,这里有更多来自RcppBDT的内容(实际上,它主要是Rcpp模块的测试用例)。

R> library(RcppBDT)
Loading required package: Rcpp
R> 
R> str(bdt)
Reference class 'Rcpp_date' [package ".GlobalEnv"] with 0 fields
 and 42 methods, of which 31 are possibly relevant:
   addDays, finalize, fromDate, getDate, getDay, getDayOfWeek, getDayOfYear, 
   getEndOfBizWeek, getEndOfMonth, getFirstDayOfWeekAfter,
   getFirstDayOfWeekInMonth, getFirstOfNextMonth, getIMMDate, getJulian, 
   getLastDayOfWeekBefore, getLastDayOfWeekInMonth, getLocalClock, getModJulian,
   getMonth, getNthDayOfWeek, getUTC, getWeekNumber, getYear, initialize, 
   setEndOfBizWeek, setEndOfMonth, setFirstOfNextMonth, setFromLocalClock,
   setFromUTC, setIMMDate, subtractDays
R> bdt$fromDate( as.Date("2001-01-01") )
R> bdt$addDays( 45 )
R> print(bdt)
[1] "2001-02-15"
R> 

#1


61  

Use +

使用+

> as.Date("2001-01-01") + 45
[1] "2001-02-15"

#2


21  

You could also use

您还可以使用

library(lubridate)
dmy("1/1/2001") + days(45)

#3


9  

In addition to the simple addition shown by others, you can also use seq.Date or seq.POSIXt to find other increments or decrements (the POSIXt version does seconds, minutes, hours, etc.):

除了其他人展示的简单添加之外,您还可以使用seq。日期或seq。查找其他增量或递减(POSIXt版本执行秒、分钟、小时等):

> seq.Date( Sys.Date(), length=2, by='3 months' )[2]
[1] "2012-07-25"

#4


9  

Just use

只使用

 as.Date("2001-01-01") + 45

from base R, or date functionality in one of the many contributed packages. My RcppBDT package wraps functionality from Boost Date_Time including things like 'date of third Wednesday' in a given month.

从基本的R,或日期功能,在其中一个许多贡献包。我的RcppBDT包包含Boost Date_Time的功能,包括给定月份的“第三个星期三”。

Edit: And egged on by @Andrie, here is a bit more from RcppBDT (which is mostly a test case for Rcpp modules, really).

编辑:@Andrie推动下,这里有更多来自RcppBDT的内容(实际上,它主要是Rcpp模块的测试用例)。

R> library(RcppBDT)
Loading required package: Rcpp
R> 
R> str(bdt)
Reference class 'Rcpp_date' [package ".GlobalEnv"] with 0 fields
 and 42 methods, of which 31 are possibly relevant:
   addDays, finalize, fromDate, getDate, getDay, getDayOfWeek, getDayOfYear, 
   getEndOfBizWeek, getEndOfMonth, getFirstDayOfWeekAfter,
   getFirstDayOfWeekInMonth, getFirstOfNextMonth, getIMMDate, getJulian, 
   getLastDayOfWeekBefore, getLastDayOfWeekInMonth, getLocalClock, getModJulian,
   getMonth, getNthDayOfWeek, getUTC, getWeekNumber, getYear, initialize, 
   setEndOfBizWeek, setEndOfMonth, setFirstOfNextMonth, setFromLocalClock,
   setFromUTC, setIMMDate, subtractDays
R> bdt$fromDate( as.Date("2001-01-01") )
R> bdt$addDays( 45 )
R> print(bdt)
[1] "2001-02-15"
R>