Dates & Times

Format codes: %Y year, %m month, %d day, %H hour, %M minute, %S second, %b/%B month name, %a/%A weekday, %j day of year, %U week, %Z time zone.

16.1 Base R

Format codes: %Y year, %m month, %d day, %H hour, %M minute, %S second, %b/%B month name, %a/%A weekday, %j day of year, %U week, %Z time zone.

today <- Sys.Date()                      # class "Date"
now <- Sys.time()                        # class "POSIXct"
d <- as.Date("2026-10-25")
as.Date("25/10/2026", format = "%d/%m/%Y")
format(d, "%A, %d %B %Y")                # "Sunday, 25 October 2026"
weekdays(d); months(d); julian(d)
d + 30                                   # add days
difftime(as.Date("2027-01-01"), d, units = "days")
seq(d, by = "month", length.out = 3)
as.numeric(d)                            # days since 1970-01-01
t <- as.POSIXct("2026-10-25 14:30:00", tz = "UTC")
format(t, tz = "Asia/Kolkata", usetz = TRUE)
strftime(t, "%H:%M"); strptime("10:30", "%H:%M")
unclass(t)                               # seconds since epoch
cut(dates, "month")                      # group by month

16.2 lubridate

Periods (months(1), calendar-aware) vs durations (ddays(1), exact seconds) vs intervals (start–end spans).

Other: hms (time of day), clock (strict, fast date-time arithmetic), tsibble (time series data frames).

library(lubridate)
ymd("2026-10-25"); dmy("25/10/2026"); mdy("October 25, 2026")
ymd_hms("2026-10-25 14:30:00", tz = "Europe/Oslo")
year(d); month(d, label = TRUE); day(d); wday(d, label = TRUE); yday(d); quarter(d)
d + days(10); d + months(1); d - years(2)
floor_date(now(), "week"); ceiling_date(now(), "month"); round_date(now(), "hour")
interval(ymd("2026-01-01"), ymd("2026-12-31")) / days(1)    # length in days
as.duration(ymd("2027-01-01") - d)
with_tz(now(), "America/New_York"); force_tz(now(), "UTC")
today() %within% interval(ymd("2026-01-01"), ymd("2026-12-31"))
make_date(2026, 10, 25)