Learn R Programming

DescTools (version 0.99.7)

Year: Part of Date

Description

Returns the specific part of a given date x.

Usage

Year(x)
Quarter(x)
Month(x, format = c("num", "abbr", "full"), lang = c("local", "engl"), 
      stringsAsFactors = TRUE)
Week(x)
Day(x)
Weekday(x, format = c("num", "abbr", "full"), lang = c("local", "engl"), 
        stringsAsFactors = TRUE)
Yearday(x)
Yearmonth(x)

Day(x) <- value

IsWeekend(x)

Arguments

x
the date to be evaluated.
format
defines how the month or the weekday are to be formatted. Defaults to "num" and can be abbreviated. Is ignored for other functions.
value
new value
lang
the language for the months and daynames. This can be either the current locale (default) or english.
stringsAsFactors
logical. Defines if the result should be coerced to a factor, using the local definitions as levels. The result would be an ordered factor. Default is TRUE.

Value

  • a vector of the same dimension as x, consisting of either numeric values or characters depending on the function used.

Details

These functions are only convenience wrappers for format() and its strange codes... Based on the requested time component, the output is as follows: Year returns the year of the input date in yyyy format. Quarter returns the quarter of the year (1 to 4) for the input date. Month returns the month of the year (1 to 12) for the input date. Week returns the week of the year for the input date (0 to 53), as defined in ISO8601. Weekday returns the week day of the input date. (1 - Monday, 2 - Tuesday, ... 7 - Sunday). (Names and abbreviations in the current locale!) Yearday returns the day of the year numbering (1 to 366). Day returns the day of the month (1 to 31). Yearmonth returns the yearmonth representation (YYYYMM) of a date as longinteger. IsWeekend returns TRUE, if the date x falls on a weekend. The day can not only be extracted, but as well defined. See examples.

See Also

strptime, DateTimeClasses, as.POSIXlt

Examples

Run this code
x <- Sys.Date()

Year(x)
Quarter(x)

Month(x)
Month(x, format = "abb", lang="engl")
Month(x, format = "abb", lang="local")
Month(x, format = "full", lang="engl")
Month(x, format = "full", lang="local")

Week(x)

Day(x)
Day(x) <- 20
x

Weekday(x)
Weekday(x, format = "abb", lang="engl")
Weekday(x, format = "abb", lang="local")
Weekday(x, format = "full", lang="engl")
Weekday(x, format = "full", lang="local")

Yearday(x)

IsWeekend(x)

# let's generate a time sequence by weeks
Month(seq(from=as.Date(Sys.Date()), to=Sys.Date()+150, by="weeks"), format="a")

Run the code above in your browser using DataLab