R: Date Class (original) (raw)

Dates {base} R Documentation

Description

Description of the class "Date" representing calendar dates.

Usage

## S3 method for class 'Date'
summary(object, digits = 12, ...)

## S3 method for class 'Date'
print(x, max = NULL, ...)

Arguments

object, x a Date object to be summarized or printed.
digits number of significant digits for the computations.
max numeric or NULL, specifying the maximal number of entries to be printed. By default, when NULL,getOption("max.print") used.
... further arguments to be passed from or to other methods.

Details

Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates. They are always printed following the rules of the current Gregorian calendar, even though that calendar was not in use long ago (it was adopted in 1752 in Great Britain and its colonies). When printing there is assumed to be a year zero.

It is intended that the date should be an integer value, but this is not enforced in the internal representation. Fractional days will be ignored when printing. It is possible to produce fractional days via the mean method or by adding or subtracting (see[Ops.Date](../../base/help/Ops.Date.html)).

When a date is converted to a date-time (for example by[as.POSIXct](../../base/help/as.POSIXct.html) or [as.POSIXlt](../../base/help/as.POSIXlt.html) its time is taken as midnight in UTC.

Printing dates involves conversion to class "[POSIXlt](../../base/help/POSIXlt.html)"which treats dates of more than about 780 billion days (2.1 billion years) from present as NA.

For the many methods see methods(class = "Date"). Several are documented separately, see below.

Warning

Do not use [identical](../../base/help/identical.html)() on objects of class "Date". Their [storage.mode](../../base/help/storage.mode.html) may be "double" or"integer", and which is chosen has depended on the version ofR used to create the object.

See Also

[Sys.Date](../../base/help/Sys.Date.html) for the current date.

[weekdays](../../base/help/weekdays.html) for convenience extraction functions.

Methods with extra arguments and documentation:

[Ops.Date](../../base/help/Ops.Date.html)

for operators on "Date" objects.

[format.Date](../../base/help/format.Date.html)

for conversion to and from character strings.

[axis.Date](../../graphics/html/axis.POSIXct.html)

and [hist.Date](../../graphics/html/hist.POSIXt.html) for plotting.

[seq.Date](../../base/help/seq.Date.html)

, [cut.Date](../../base/help/cut.Date.html), and[round.Date](../../base/help/round.Date.html) for utility operations.

[DateTimeClasses](../../base/help/DateTimeClasses.html) for date-time classes.

Examples


(today <- Sys.Date())
format(today, "%d %b %Y")  # with month as a word
(tenweeks <- seq(today, length.out=10, by="1 week")) # next ten weeks
weekdays(today)
months(tenweeks)

(Dls <- as.Date(.leap.seconds))

## Show use of year zero:
(z <- as.Date("01-01-01")) # how it is printed depends on the OS
z - 365 # so year zero was a leap year.
as.Date("00-02-29")
# if you want a different format, consider something like (if supported)
## Not run: format(z, "%04Y-%m-%d") # "0001-01-01"
format(z, "%_4Y-%m-%d") # "   1-01-01"
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.6.0 Index]