Learn R Programming

ds4psy (version 0.7.0)

days_in_month: 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()).

Value

A named (numeric) vector.

Details

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

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()

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataLab