Learn R Programming

ds4psy (version 0.4.0)

is_leap_year: Is some year a so-called leap year?

Description

is_leap_year checks whether a given year (provided as a date or time dt, or number/string denoting a 4-digit year) lies in a so-called leap year (i.e., a year containing a date of Feb-29).

Usage

is_leap_year(dt)

Arguments

dt

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

Details

When dt is not recognized as "Date" or "POSIXt" object(s), is_leap_year aims to parse a string dt as describing year(s) in a "dddd" (4-digit year) format, as a valid "Date" string (to retrieve the 4-digit year "%Y"), or a numeric dt as 4-digit integer(s).

is_leap_year then solves the task in two ways: 1. by verifying the numeric definition of a "leap year", and 2. by trying to use as.Date() for defining a "Date" of Feb-29 in the corresponding year(s).

See Also

leap_year function of the lubridate package.

Other date and time functions: change_time(), change_tz(), cur_date(), cur_time(), what_date(), what_day(), what_month(), what_time(), what_week(), what_year()

Examples

Run this code
# NOT RUN {
is_leap_year(2020)

# from dates:
is_leap_year(Sys.Date())
is_leap_year(as.Date("2022-02-28"))

# from times:
is_leap_year(Sys.time())
is_leap_year(as.POSIXct("2022-10-11 10:11:12"))
is_leap_year(as.POSIXlt("2022-10-11 10:11:12"))

# from non-integers:
is_leap_year(2019.5)

# For vectors:
is_leap_year(2020:2028)

# with dt as strings:
is_leap_year(c("2020", "2021"))
is_leap_year(c("2020-02-29 01:02:03", "2021-02-28 01:02"))
# Note: Invalid date string would yield error
# is_leap_year("2021-02-29")


# }

Run the code above in your browser using DataLab