TExcelTAD
Home › R › R Dates

R Dates

R stores calendar dates in the Date class and date-times in POSIXct. With them you can sort dates, compute differences, add days and extract parts such as the weekday.

Creating dates

as.Date() converts text in ISO format ("YYYY-MM-DD") into a date. Internally a Date is the number of days since 1 January 1970:

r · try it yourself
launch <- as.Date("2024-03-15")
launch
class(launch)
as.numeric(launch)
as.Date(0)
[1] "2024-03-15"
[1] "Date"
[1] 19797
[1] "1970-01-01"

Note: Sys.Date() returns today's date and Sys.time() the current date-time. We avoid them in this course so that every example prints the same output each time it runs.

Other formats

For other layouts, describe the format with % codes:

Code Meaning Example
%Y 4-digit year 2024
%y 2-digit year 24
%m month number 03
%B / %b month name / abbreviated March / Mar
%d day of month 15
%A / %a weekday name / abbreviated Friday / Fri
%H:%M:%S hours, minutes, seconds 14:30:00
r · try it yourself
as.Date("15/03/2024", format = "%d/%m/%Y")
as.Date("March 15, 2024", format = "%B %d, %Y")
d <- as.Date("2024-03-15")
format(d, "%d %B %Y")
format(d, "%A")
weekdays(d)
months(d)
[1] "2024-03-15"
[1] "2024-03-15"
[1] "15 March 2024"
[1] "Friday"
[1] "Friday"
[1] "March"

Date arithmetic

Adding a number adds days. Subtracting two dates gives a difftime:

r · try it yourself
start <- as.Date("2024-01-10")
end <- as.Date("2024-03-01")
end - start
as.numeric(end - start)
difftime(end, start, units = "weeks")
start + 30
start > end
Time difference of 51 days
[1] 51
Time difference of 7.285714 weeks
[1] "2024-02-09"
[1] FALSE

2024 is a leap year, which R takes into account automatically.

Sequences of dates

seq() works with dates and accepts steps like "day", "week", "month" or "year":

r · try it yourself
seq(as.Date("2024-01-31"), by = "month", length.out = 4)
seq(as.Date("2024-05-01"), as.Date("2024-05-29"), by = "week")
[1] "2024-01-31" "2024-03-02" "2024-03-31" "2024-05-01"
[1] "2024-05-01" "2024-05-08" "2024-05-15" "2024-05-22" "2024-05-29"

Note how "one month after 31 January" behaves: R keeps the day number 31, and because 31 February doesn't exist, that date rolls over into March (2 March in the leap year 2024). Start monthly sequences on day 1 to avoid surprises.

Extracting parts

format() returns parts as text; convert them with as.integer() for calculations. as.POSIXlt() exposes the components as numbers (years counted from 1900, months from 0):

r · try it yourself
dates <- as.Date(c("2023-12-24", "2024-02-29", "2024-07-04"))
as.integer(format(dates, "%Y"))
as.integer(format(dates, "%m"))
lt <- as.POSIXlt(dates)
lt$year + 1900
lt$yday + 1        # day of the year
[1] 2023 2024 2024
[1] 12  2  7
[1] 2023 2024 2024
[1] 358  60 186

Date-times

as.POSIXct() stores a date and time as seconds since 1970. Always give a time zone (tz) to make results reproducible:

r · try it yourself
meeting <- as.POSIXct("2024-03-15 14:30:00", tz = "UTC")
meeting
meeting + 90 * 60               # add 90 minutes
format(meeting, "%H:%M")
difftime(as.POSIXct("2024-03-15 18:00", tz = "UTC"), meeting, units = "hours")
[1] "2024-03-15 14:30:00 UTC"
[1] "2024-03-15 16:00:00 UTC"
[1] "14:30"
Time difference of 3.5 hours

Grouping by date

r · try it yourself
sales <- data.frame(
  day = as.Date(c("2024-01-05", "2024-01-20", "2024-02-03", "2024-02-17", "2024-03-09")),
  amount = c(120, 80, 150, 90, 200)
)
sales$month <- format(sales$day, "%Y-%m")
aggregate(amount ~ month, data = sales, FUN = sum)
    month amount
1 2024-01    200
2 2024-02    240
3 2024-03    200

Test yourself

What does as.Date("2024-01-01") + 31 give?

  1. "2024-01-31"
  2. "2024-02-01"
  3. "2024-01-32"
  4. An error

Which format string parses "25/12/2024"?

  1. %Y-%m-%d
  2. %d/%m/%Y
  3. %m/%d/%Y
  4. %d-%m-%y

What class does subtracting two dates produce?

  1. numeric
  2. Date
  3. difftime
  4. character