Add one year to a posix time

BobLeGob picture BobLeGob · Jan 28, 2016 · Viewed 10.6k times · Source

I have a date like "2016-01-01" (YYYY-MM-DD) and I'm using as.numeric(as.POSIXct(...)) to use it as an Integer.

My question is, is there a way to add to this date a year, a month or a day ? I mean, if I add one year to 2016 it wont be the same as adding a year to 2015 (bissextile stuff).

Same as adding 32 days to January 01 wont be the same as adding 32 days to February 01 (because of number of days that may change)

I managed to have something that works for years and months but I'd like to implement days as well

how_long_is_simul <- function(lst){
    # Format DATE_START
    greg_date = intDate_to_gregorianDate(DATE_START)
    reg = "^([0-9]{4})\\-([0-9]{2})\\-([0-9]{2})$" # black-magic
    splited_date = str_match(greg_date, reg)

    # Manage months limit
    lst$years = lst$years + floor(lst$months/12)
    lst$months = lst$months%%12

    # Build new date
    my_vector = c(lst$years, lst$months, 0)
    end_date = paste(as.numeric(splited_date[2:4]) + my_vector, collapse = "-")
    return(round((gregorianDate_to_intDate(end_date)-DATE_START)/86400))
}

# AND the vars used by the function
DATE_START <- 1451606400 # 2016-01-01 GMT
lst   = list( # no days, because of bissextile years
    years  = 1,
    months = 0
)

Basically what I'm doing is from a DATE_START integer transform it into gregorian then add the months / years from lst then rebuild a clean string and reconvert it to integer.

N.B. Conversion int <---> gregorian are done with POSIXct

I'm not sure if I explained it well, but thank you anyway :)

Answer

Jaap picture Jaap · Jan 28, 2016

Adding a day, month or year is quite straightforward with %m+% from the lubridate package:

library(lubridate)

x <- as.Date("2016-01-01")
x %m+% days(1)

which gives:

[1] "2016-01-02"

For adding months or years, you can use months or years instead of days:

> x %m+% months(1)
[1] "2016-02-01"
> x %m+% years(1)
[1] "2017-01-01"