Skip to contents

as_yearmonth() is a generic for coercing input in to <grates_yearmonth>. Character input is first parsed using as.Date(). POSIXct and POSIXlt are all converted, with the timezone respected.

Usage

as_yearmonth(x, ...)

# S3 method for default
as_yearmonth(x, ...)

# S3 method for Date
as_yearmonth(x, ...)

# S3 method for POSIXt
as_yearmonth(x, ...)

# S3 method for character
as_yearmonth(x, ...)

# S3 method for factor
as_yearmonth(x, ...)

Arguments

x

R object.

...

Only used For character input where additional arguments are passed through to as.Date().

Value

A <grates_yearmonth> object.

Note

Internally <grates_yearmonth> objects are stored as the number of months (starting at 0) since the Unix Epoch (1970-01-01). Precision is only to the month level (i.e. the day of the month is always dropped).

References

The algorithm to convert between dates and months relative to the UNIX Epoch comes from the work of Davis Vaughan in the unreleased datea package.

See also

Examples

as_yearmonth(Sys.Date())
#> <grates_yearmonth[1]>
#> [1] "2024-Mar"
as_yearmonth(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"), interval = 2)
#> <grates_yearmonth[1]>
#> [1] "2019-Mar"
as_yearmonth("2019-05-03")
#> <grates_yearmonth[1]>
#> [1] "2019-May"