R Date and Time
Dates and times are a special data type in R. They look like strings ("2024-08-15") but behave like numbers — you can subtract two dates to get the number of days between them, add days to a date, and sort a dataset chronologically. R has three main date and time classes.
Date and Time Classes in R
Class Stores Example ────────────────────────────────────────────────────────── Date Date only "2024-08-15" POSIXct Date + Time (numeric) "2024-08-15 14:30:00" POSIXlt Date + Time (list) has named parts (sec, min, hour...)
Creating Date Values
today <- Sys.Date() # today's date
print(today) # "2024-08-15"
class(today) # "Date"
# From string
dob <- as.Date("1995-06-20") # ISO format (default)
event <- as.Date("15/08/2024", format="%d/%m/%Y") # custom format
Date Format Codes
Code Meaning Example ─────────────────────────────────────────── %Y 4-digit year 2024 %y 2-digit year 24 %m Month (01-12) 08 %B Full month name August %b Abbrev month Aug %d Day (01-31) 15 %A Full weekday Thursday %a Abbrev weekday Thu
Date Arithmetic
today <- as.Date("2024-08-15")
birthday <- as.Date("2024-10-05")
birthday - today # 51 days
today + 30 # "2024-09-14"
today - 365 # "2023-08-15"
# Days between two dates
diff_days <- as.numeric(birthday - today)
cat("Days until birthday:", diff_days, "\n")
Extracting Date Parts
d <- as.Date("2024-08-15")
format(d, "%Y") # "2024" year
format(d, "%m") # "08" month
format(d, "%d") # "15" day
format(d, "%A") # "Thursday" weekday
weekdays(d) # "Thursday"
months(d) # "August"
quarters(d) # "Q3"
Date Sequences
# Daily sequence
seq(as.Date("2024-01-01"), as.Date("2024-01-07"), by="day")
# Monthly sequence
seq(as.Date("2024-01-01"), as.Date("2024-12-01"), by="month")
# Weekly
seq(as.Date("2024-01-01"), by="week", length.out=6)
Working with Date and Time (POSIXct)
now <- Sys.time()
print(now)
# "2024-08-15 14:30:45 IST"
# Create from string
event_time <- as.POSIXct("2024-08-15 09:00:00",
tz = "Asia/Kolkata")
# Extract parts
format(event_time, "%H:%M") # "09:00"
format(event_time, "%I %p") # "09 AM"
# Time difference
end <- as.POSIXct("2024-08-15 17:30:00")
start <- as.POSIXct("2024-08-15 09:00:00")
difftime(end, start, units="hours") # 8.5 hours
Practical: Age Calculator
birth_date <- as.Date("1990-05-22")
today <- Sys.Date()
years <- as.numeric(today - birth_date) %/% 365
cat("Age:", years, "years\n")
lubridate Package (Recommended)
library(lubridate)
ymd("2024-08-15") # Date from YYYY-MM-DD
dmy("15-08-2024") # Date from DD-MM-YYYY
mdy("08/15/2024") # Date from MM-DD-YYYY
year(today) # 2024
month(today) # 8
day(today) # 15
wday(today, label=TRUE) # "Thu"
today %m+% months(3) # add 3 months safely
today %m-% years(1) # subtract 1 year
Date and time handling appears in virtually every real dataset — transaction logs, sensor readings, survey timestamps, and sales records all carry date information. Mastering date arithmetic, formatting, and extraction lets you create time-based analyses like monthly trends, duration calculations, and age groupings.
