Search code examples
rlubridate

chr NAs appearing when creating dynamic date objects


I've got a very intermittent problem when creating some dynamic date objects.

library(lubridate)
day <- as.Date(Sys.time())
this_m <- format(day,"%m")
next_m <- format(day + months(1), "%m")
m_after <- format(day + months(2), "%m")

For some reason the next_m object delivers an object that is considered 'chr NA' while the m_after object delivers the correct value (as of today, a character object of "03"). This seems very strange to me, why might this be?

Edit

Ideally I'm create an object that has the numeric version of the month in two digits, e.g March is 03 rather than just 3


Solution

  • So I was able to fix doing the following:

    next_m  <- month(day %m+% months(1), "%m")
    m_after <- month(day %m+% months(2), "%m")