Examples for 'base::Date'


Date Class

Aliases: Date Dates print.Date summary.Date Math.Date Summary.Date [.Date [<-.Date length<-.Date [[.Date as.data.frame.Date as.list.Date c.Date mean.Date split.Date

Keywords: utilities chron

### ** Examples
## No test: 
(today <- Sys.Date())
[1] "2025-08-11"
format(today, "%d %b %Y")  # with month as a word
[1] "11 Aug 2025"
(tenweeks <- seq(today, length.out=10, by="1 week")) # next ten weeks
 [1] "2025-08-11" "2025-08-18" "2025-08-25" "2025-09-01" "2025-09-08"
 [6] "2025-09-15" "2025-09-22" "2025-09-29" "2025-10-06" "2025-10-13"
weekdays(today)
[1] "Monday"
months(tenweeks)
 [1] "August"    "August"    "August"    "September" "September" "September"
 [7] "September" "September" "October"   "October"  
## End(No test)
(Dls <- as.Date(.leap.seconds))
 [1] "1972-07-01" "1973-01-01" "1974-01-01" "1975-01-01" "1976-01-01"
 [6] "1977-01-01" "1978-01-01" "1979-01-01" "1980-01-01" "1981-07-01"
[11] "1982-07-01" "1983-07-01" "1985-07-01" "1988-01-01" "1990-01-01"
[16] "1991-01-01" "1992-07-01" "1993-07-01" "1994-07-01" "1996-01-01"
[21] "1997-07-01" "1999-01-01" "2006-01-01" "2009-01-01" "2012-07-01"
[26] "2015-07-01" "2017-01-01"
## Show use of year zero:
(z <- as.Date("01-01-01")) # how it is printed depends on the OS
[1] "1-01-01"
z - 365 # so year zero was a leap year.
[1] "0-01-02"
as.Date("00-02-29")
[1] "0-02-29"
# if you want a different format, consider something like (if supported)
## Not run: 
##D format(z, "%04Y-%m-%d") # "0001-01-01"
##D format(z, "%_4Y-%m-%d") # "   1-01-01"
##D format(z, "%_Y-%m-%d")  # "1-01-01"
## End(Not run)

##  length(<Date>) <- n   now works
ls <- Dls; length(ls) <- 12
l2 <- Dls; length(l2) <- 5 + length(Dls)
stopifnot(exprs = {
  ## length(.) <- * is compatible to subsetting/indexing:
  identical(ls, Dls[seq_along(ls)])
  identical(l2, Dls[seq_along(l2)])
  ## has filled with NA's
  is.na(l2[(length(Dls)+1):length(l2)])
})

[Package base version 4.2.3 Index]