lubridate Exercises in R: 50 Real Practice Problems

Fifty practice problems on lubridate: parse, extract components, do date arithmetic, work with intervals and time zones. Hidden solutions.

RRun this once before any exercise
library(lubridate) library(dplyr) library(tibble)

  

Section 1. Parsing dates (8 problems)

Exercise 1.1: ymd

Difficulty: Beginner. Parse "2024-01-15".

Show solution
RInteractive R
ymd("2024-01-15")

  

Exercise 1.2: mdy

Difficulty: Beginner. Parse "01/15/2024".

Show solution
RInteractive R
mdy("01/15/2024")

  

Exercise 1.3: dmy

Difficulty: Beginner. Parse "15-01-2024".

Show solution
RInteractive R
dmy("15-01-2024")

  

Exercise 1.4: ymd_hms

Difficulty: Intermediate. Parse "2024-01-15 14:30:00".

Show solution
RInteractive R
ymd_hms("2024-01-15 14:30:00")

  

Exercise 1.5: parse_date_time multi-format

Difficulty: Intermediate. Parse a vector with mixed formats.

Show solution
RInteractive R
parse_date_time(c("2024-01-15","01/15/2024","Jan 15, 2024"), orders = c("ymd","mdy","mdY"))

  

Exercise 1.6: From milliseconds since epoch

Difficulty: Advanced. Convert 1705305600 to POSIXct.

Show solution
RInteractive R
as_datetime(1705305600)

  

Exercise 1.7: Parse with timezone

Difficulty: Advanced. Parse and set NY timezone.

Show solution
RInteractive R
ymd_hms("2024-01-15 09:00:00", tz = "America/New_York")

  

Exercise 1.8: From a numeric like 20240115

Difficulty: Intermediate. Parse 20240115 as a date.

Show solution
RInteractive R
ymd(20240115)

  

Section 2. Extract components (8 problems)

Exercise 2.1: Year

Difficulty: Beginner.

Show solution
RInteractive R
year(ymd("2024-04-15"))

  

Exercise 2.2: Month with label

Difficulty: Intermediate. Full month name.

Show solution
RInteractive R
month(ymd("2024-04-15"), label = TRUE, abbr = FALSE)

  

Exercise 2.3: Day of month

Difficulty: Beginner.

Show solution
RInteractive R
day(ymd("2024-04-15"))

  

Exercise 2.4: Day of week

Difficulty: Intermediate. With Monday-first abbreviated label.

Show solution
RInteractive R
wday(ymd("2024-04-15"), label = TRUE, week_start = 1)

  

Exercise 2.5: Day of year (julian)

Difficulty: Intermediate.

Show solution
RInteractive R
yday(ymd("2024-04-15"))

  

Exercise 2.6: Quarter

Difficulty: Intermediate.

Show solution
RInteractive R
quarter(ymd("2024-04-15"))

  

Exercise 2.7: Week of year

Difficulty: Intermediate.

Show solution
RInteractive R
week(ymd("2024-04-15"))

  

Exercise 2.8: Hour, minute, second

Difficulty: Intermediate.

Show solution
RInteractive R
ts <- ymd_hms("2024-04-15 14:30:45") c(h = hour(ts), m = minute(ts), s = second(ts))

  

Section 3. Arithmetic (8 problems)

Exercise 3.1: Add days

Difficulty: Beginner. Add 7 days.

Show solution
RInteractive R
ymd("2024-01-15") + days(7)

  

Exercise 3.2: Add weeks

Difficulty: Beginner. Add 4 weeks.

Show solution
RInteractive R
ymd("2024-01-15") + weeks(4)

  

Exercise 3.3: Add months

Difficulty: Intermediate. Add 3 months. Note period vs duration.

Show solution
RInteractive R
ymd("2024-01-15") + months(3)

  

Exercise 3.4: Subtract years

Difficulty: Intermediate. 5 years ago today.

Show solution
RInteractive R
today() - years(5)

  

Exercise 3.5: Difference in days

Difficulty: Intermediate. Days between two dates.

Show solution
RInteractive R
as.integer(ymd("2024-04-15") - ymd("2024-01-01"))

  

Exercise 3.6: Age in years

Difficulty: Intermediate. Age from birth date.

Show solution
RInteractive R
as.integer(interval(ymd("1990-05-20"), today()) / years(1))

  

Exercise 3.7: Add business days (approximation)

Difficulty: Advanced. Add 10 weekdays.

Show solution
RInteractive R
add_with_rollback(ymd("2024-01-15"), days(14)) # rough; for true business days, bizdays pkg

  

Exercise 3.8: Roll to month end

Difficulty: Advanced.

Show solution
RInteractive R
ceiling_date(ymd("2024-02-10"), "month") - days(1)

  

Section 4. Intervals and durations (8 problems)

Exercise 4.1: Interval object

Difficulty: Intermediate. Build an interval Jan 1 to Apr 30.

Show solution
RInteractive R
interval(ymd("2024-01-01"), ymd("2024-04-30"))

  

Exercise 4.2: Duration in days

Difficulty: Intermediate. Same span as days.

Show solution
RInteractive R
as.duration(interval(ymd("2024-01-01"), ymd("2024-04-30"))) / ddays(1)

  

Exercise 4.3: Check overlap

Difficulty: Advanced. Do two intervals overlap?

Show solution
RInteractive R
i1 <- interval(ymd("2024-01-01"), ymd("2024-04-30")) i2 <- interval(ymd("2024-04-15"), ymd("2024-06-30")) int_overlaps(i1, i2)

  

Exercise 4.4: Period vs duration

Difficulty: Advanced. Show difference using months().

Show solution
RInteractive R
ymd("2024-01-31") + months(1) # period: Feb 29 (leap-aware) ymd("2024-01-31") + dmonths(1) # duration: ~Mar 2 (fixed seconds)

  

Exercise 4.5: Check if date in interval

Difficulty: Intermediate. Is "2024-03-15" inside Jan-Apr?

Show solution
RInteractive R
ymd("2024-03-15") %within% interval(ymd("2024-01-01"), ymd("2024-04-30"))

  

Exercise 4.6: Length in months

Difficulty: Advanced.

Show solution
RInteractive R
i <- interval(ymd("2024-01-15"), ymd("2024-07-31")) i %/% months(1)

  

Exercise 4.7: Sum durations

Difficulty: Intermediate. Add 2 hours and 30 minutes.

Show solution
RInteractive R
hours(2) + minutes(30)

  

Exercise 4.8: Days remaining in month

Difficulty: Advanced.

Show solution
RInteractive R
d <- ymd("2024-04-15") as.integer(ceiling_date(d, "month") - 1 - d)

  

Section 5. Floor and round (8 problems)

Exercise 5.1: Floor to month

Difficulty: Intermediate.

Show solution
RInteractive R
floor_date(ymd("2024-04-15"), "month")

  

Exercise 5.2: Floor to week (Mon start)

Difficulty: Intermediate.

Show solution
RInteractive R
floor_date(ymd("2024-04-15"), "week", week_start = 1)

  

Exercise 5.3: Ceiling to month

Difficulty: Intermediate.

Show solution
RInteractive R
ceiling_date(ymd("2024-04-15"), "month")

  

Exercise 5.4: Round to nearest hour

Difficulty: Intermediate.

Show solution
RInteractive R
round_date(ymd_hms("2024-04-15 14:35:00"), "hour")

  

Exercise 5.5: Floor to quarter

Difficulty: Intermediate.

Show solution
RInteractive R
floor_date(ymd("2024-04-15"), "quarter")

  

Exercise 5.6: Group by month

Difficulty: Intermediate. Aggregate counts per month.

Show solution
RInteractive R
df <- tibble(date = as.Date(c("2024-01-05","2024-01-20","2024-02-10")), n = c(1, 2, 3)) df |> mutate(m = floor_date(date, "month")) |> group_by(m) |> summarise(n = sum(n))

  

Exercise 5.7: Floor to 15-minute bucket

Difficulty: Advanced.

Show solution
RInteractive R
floor_date(ymd_hms("2024-04-15 14:37:21"), "15 mins")

  

Exercise 5.8: Round to nearest day

Difficulty: Intermediate.

Show solution
RInteractive R
round_date(ymd_hms("2024-04-15 13:00:00"), "day")

  

Section 6. Time zones (5 problems)

Exercise 6.1: Set time zone

Difficulty: Intermediate. Force NY timezone.

Show solution
RInteractive R
force_tz(ymd_hms("2024-04-15 09:00:00"), "America/New_York")

  

Exercise 6.2: Convert time zone

Difficulty: Advanced. NY -> Tokyo.

Show solution
RInteractive R
ts <- ymd_hms("2024-04-15 09:00:00", tz = "America/New_York") with_tz(ts, "Asia/Tokyo")

  

Exercise 6.3: List available zones

Difficulty: Intermediate.

Show solution
RInteractive R
head(OlsonNames(), 10)

  

Exercise 6.4: Daylight saving check

Difficulty: Advanced. Is "2024-07-01 NY" in DST?

Show solution
RInteractive R
dst(ymd_hms("2024-07-01 12:00:00", tz = "America/New_York"))

  

Exercise 6.5: Convert to UTC

Difficulty: Intermediate.

Show solution
RInteractive R
with_tz(ymd_hms("2024-04-15 09:00:00", tz = "America/New_York"), "UTC")

  

Section 7. Real workflows (5 problems)

Exercise 7.1: Compute days between events

Difficulty: Intermediate. Per user, days from first to last event.

Show solution
RInteractive R
events <- tibble(user = c("a","a","b","b"), date = ymd(c("2024-01-05","2024-03-10","2024-02-01","2024-04-15"))) events |> group_by(user) |> summarise(days = as.integer(max(date) - min(date)))

  

Exercise 7.2: Build a daily calendar

Difficulty: Intermediate. All dates in Q1 2024.

Show solution
RInteractive R
seq.Date(ymd("2024-01-01"), ymd("2024-03-31"), by = "day")

  

Exercise 7.3: Detect weekends

Difficulty: Beginner. Flag a date as weekend.

Show solution
RInteractive R
wday(ymd("2024-04-13"), week_start = 1) %in% c(6, 7)

  

Exercise 7.4: Group transactions by month-end

Difficulty: Advanced. Tag each row with its month-end.

Show solution
RInteractive R
df <- tibble(date = ymd(c("2024-01-15","2024-02-20","2024-03-05")), amt = c(50,80,30)) df |> mutate(month_end = ceiling_date(date, "month") - 1)

  

Exercise 7.5: Time-of-day buckets

Difficulty: Intermediate. Tag hour as morning/afternoon/evening.

Show solution
RInteractive R
ts <- ymd_hms(c("2024-04-15 08:30:00","2024-04-15 14:00:00","2024-04-15 21:00:00")) case_when(hour(ts) < 12 ~ "morning", hour(ts) < 18 ~ "afternoon", TRUE ~ "evening")

  

What to do next

  • Date-Time-Manipulation-Exercises (coming), broader date workflows.
  • tidyverse-Exercises (shipped), dates inside data pipelines.
  • Time-Series-Exercises (coming), date-indexed analysis.