days_in_month: How many days are in a month (of given date)?

View source: R/time_util_fun.R

days_in_monthR Documentation

How many days are in a month (of given date)?

Description

days_in_month computes the number of days in the months of given dates (provided as a date or time dt, or number/string denoting a 4-digit year).

Usage

days_in_month(dt = Sys.Date(), ...)

Arguments

dt

Date or time (scalar or vector). Default: dt = Sys.Date(). Numbers or strings with dates are parsed into 4-digit numbers denoting the year.

...

Other parameters (passed to as.Date()).

Details

The function requires dt as "Dates", rather than month names or numbers, to check for leap years (in which February has 29 days).

Value

A named (numeric) vector.

See Also

is_leap_year to check for leap years; diff_tz for time zone-based time differences; days_in_month function of the lubridate package.

Other date and time functions: change_time(), change_tz(), cur_date(), cur_time(), diff_dates(), diff_times(), diff_tz(), is_leap_year(), what_date(), what_month(), what_time(), what_wday(), what_week(), what_year(), zodiac()

Examples

days_in_month() 

# Robustness: 
days_in_month(Sys.Date())    # Date
days_in_month(Sys.time())    # POSIXct
days_in_month("2020-07-01")  # string
days_in_month(20200901)      # number
days_in_month(c("2020-02-10 01:02:03", "2021-02-11", "2024-02-12"))  # vectors of strings

# For leap years:
ds <- as.Date("2020-02-20") + (365 * 0:4)  
days_in_month(ds)  # (2020/2024 are leap years)


ds4psy documentation built on Sept. 15, 2023, 9:08 a.m.