Tutorial by Examples

R is able to access the current date, time and time zone: Sys.Date() # Returns date as a Date object ## [1] "2016-07-21" Sys.time() # Returns date & time at current locale as a POSIXct object ## [1] "2016-07-21 10:04:39 CDT" as.numeric(Sys...
Let's say we want to go to the last day of the month, this function will help on it: eom <- function(x, p=as.POSIXlt(x)) as.Date(modifyList(p, list(mon=p$mon + 1, mday=0))) Test: x <- seq(as.POSIXct("2000-12-10"),as.POSIXct("2001-05-10"),by="months") > da...
Let's say we want to go to the first day of a given month: date <- as.Date("2017-01-20") > as.POSIXlt(cut(date, "month")) [1] "2017-01-01 EST"
Let's say we want to move a given date a numof months. We can define the following function, that uses the mondate package: moveNumOfMonths <- function(date, num) { as.Date(mondate(date) + num) } It moves consistently the month part of the date and adjusting the day, in case the date re...

Page 1 of 1