DateFunctions.Rd
Some more date functions for making daily life a bit easier. The first ones extract a specific part of a given date, others check some conditions.
Year(x)
Quarter(x)
Month(x, fmt = c("m", "mm", "mmm"), lang = DescToolsOptions("lang"),
stringsAsFactors = TRUE)
Week(x, method = c("iso", "us"))
Day(x)
Weekday(x, fmt = c("d", "dd", "ddd"), lang = DescToolsOptions("lang"),
stringsAsFactors = TRUE)
YearDay(x)
YearMonth(x)
Day(x) <- value
IsWeekend(x)
IsLeapYear(x)
Hour(x)
Minute(x)
Second(x)
Timezone(x)
HmsToMinute(x)
Now()
Today()
DiffDays360(start_d, end_d, method = c("eu", "us"))
LastDayOfMonth(x)
YearDays(x)
MonthDays(x)
the date to be evaluated.
format string, defines how the month or the weekday are to be formatted. Defaults to "m"
, resp. "d"
. Is ignored for other functions.
new value
optional value setting the language for the months and daynames. Can be either "local"
for current locale or "engl"
for english. If left to NULL
, the option "lang"
will be searched for and if not found "local"
will be taken as default.
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.
the start, resp. end date for DiffDays360
.
one out of "eu", "us"
, setting either European or US-Method calculation mode. Default is "eu"
.
These functions are mainly convenience wrappers for the painful 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 or a yearmonth yyyymm.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 or for a yearmonth yyyymm. 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 are either english or 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 long integer. Hour
, Minute
, Second
, Timezone
return the hour, minute, second or timezone from a POSIXlt object. HmsToMinute
converts the time parts of a POSIXlt object to minutes.Today
, Now
return the current date, resp. the current date and time.
IsWeekend
returns TRUE
, if the date x falls on a weekend. IsLeapYear
returns TRUE
, if the year of the date x is a leap year.
The day can not only be extracted, but as well be defined. See examples.
DiffDays360
calculates the difference between 2 dates using the 360-days convention.LastDayOfMonth
returns the last day of the month of the given date(s).
YearDays
returns the total number of days of the given date(s).
MonthDays
returns the numer of days of the month of the given date(s).
The language in Weekday and Moth can be set with an option as well. The functions will check for an existing option named "lang" and take this value if it exists. So simply set option(lang="engl") if the results should always be reported in English.
a vector of the same dimension as x, consisting of either numeric values or characters depending on the function used.
x <- Today() # the same as Sys.Date() but maybe easier to remember..
Year(x)
#> [1] 2024
Quarter(x)
#> [1] 3
Month(x)
#> [1] 9
Month(x, fmt = "mm", lang="engl")
#> [1] Sep
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
Month(x, fmt = "mm", lang="local")
#> [1] Sep
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
Month(x, fmt = "mmm", lang="engl")
#> [1] September
#> 12 Levels: January < February < March < April < May < June < ... < December
Month(x, fmt = "mmm", lang="local")
#> [1] September
#> 12 Levels: January < February < March < April < May < June < ... < December
Week(x)
#> [1] 39
Day(x)
#> [1] 25
Day(x) <- 20
x
#> [1] "2024-09-20"
Weekday(x)
#> [1] 5
Weekday(x, fmt = "dd", lang="engl")
#> [1] Fri
#> Levels: Mon < Tue < Wed < Thu < Fri < Sat < Sun
Weekday(x, fmt = "dd", lang="local")
#> [1] Fri
#> Levels: Mon < Tue < Wed < Thu < Fri < Sat < Sun
Weekday(x, fmt = "ddd", lang="engl")
#> [1] Friday
#> 7 Levels: Monday < Tuesday < Wednesday < Thursday < Friday < ... < Sunday
Weekday(x, fmt = "ddd", lang="local")
#> [1] Friday
#> 7 Levels: Monday < Tuesday < Wednesday < Thursday < Friday < ... < Sunday
YearDay(x)
#> [1] 264
IsWeekend(x)
#> [1] FALSE
IsLeapYear(x)
#> [1] TRUE
# let's generate a time sequence by weeks
Month(seq(from=as.Date(Sys.Date()), to=Sys.Date()+150, by="weeks"), fmt="mm")
#> [1] Sep Oct Oct Oct Oct Oct Nov Nov Nov Nov Dec Dec Dec Dec Jan Jan Jan Jan Jan
#> [20] Feb Feb Feb
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
LastDayOfMonth(as.Date(c("2014-10-12","2013-01-31","2011-12-05")))
#> [1] "2014-10-31" "2013-01-31" "2011-12-31"