Dates and Times

Lecture 15

Dr. Eric Friedlander

College of Idaho
CSCI 2025 - Winter 2026

Introduction

Working with Dates and Times

  • Dates and times can be complex due to daylight saving time (DST), time zones, and all the different formats that dates can come in
  • Dates are a very special kind of data: they are kind of “quantitative” but also kind of “categorical/ordinal”
  • The lubridate package in R makes working with dates and times easier

lubridate

  • Part of the core tidyverse.
  • Provides functions for parsing, manipulating, and performing arithmetic with date-time data.

Creating Date-Times

From Strings

  • lubridate has helper functions that parse dates by identifying the order of year, month, and day components.
  • Functions are named after the order of components: ymd(), mdy(), dmy(), etc.
ymd("2017-01-31")
[1] "2017-01-31"
mdy("January 31st, 2017")
[1] "2017-01-31"
dmy("31-Jan-2017")
[1] "2017-01-31"
  • For date-times, add _hms, _hm, or _h.
ymd_hms("2017-01-31 20:11:59")
[1] "2017-01-31 20:11:59 UTC"

From Components

  • Use make_date() for dates and make_datetime() for date-times from individual components.
  • Let’s do this with the flights data!

Date-Time Components

Accessing Components

  • Functions like year(), month(), mday(), wday(), hour(), minute(), and second() extract components.
datetime <- ymd_hms("2026-07-08 12:34:56")
year(datetime)
[1] 2026
month(datetime, label = TRUE)
[1] Jul
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
wday(datetime, label = TRUE, abbr = FALSE)
[1] Wednesday
7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Modifying Components

  • You can also set components using the same accessor functions.
datetime <- ymd_hms("2026-07-08 12:34:56")
year(datetime) <- 2030
datetime
[1] "2030-07-08 12:34:56 UTC"
  • update() can modify multiple components.
update(datetime, year = 2030, month = 2, mday = 2)
[1] "2030-02-02 12:34:56 UTC"

Time Spans

Durations

  • Represent an exact number of seconds.
  • Created with functions like dseconds(), dminutes(), dhours(), ddays().
ddays(1)
[1] "86400s (~1 days)"
dyears(1)
[1] "31557600s (~1 years)"
  • Adding a duration of a day can sometimes lead to unexpected results due to DST.

Periods

  • Represent human units like weeks and months.
  • More intuitive for arithmetic.
one_am <- ymd_hms("2026-03-08 01:00:00", tz = "America/New_York")
one_am + ddays(1) # duration
[1] "2026-03-09 02:00:00 EDT"
one_am + days(1)  # period
[1] "2026-03-09 01:00:00 EDT"

Intervals

  • Represent a start and end point.
  • Useful for calculating the exact length of a time span in human units.
y2024 <- ymd("2024-01-01") %--% ymd("2025-01-01")
y2024 / days(1)
[1] 366

Time Zones

IANA Time Zones

  • R uses IANA time zones, like "America/New_York".
  • Sys.timezone() gives your current time zone.
  • OlsonNames() lists all available time zone names.

Changing Time Zones

  • with_tz(): changes how a time is displayed without changing the instant.
  • force_tz(): changes the instant in time.
x1 <- ymd_hms("2024-06-01 12:00:00", tz = "America/New_York")
with_tz(x1, "Europe/Paris")
[1] "2024-06-01 18:00:00 CEST"
force_tz(x1, "Europe/Paris")
[1] "2024-06-01 12:00:00 CEST"

Practice!

Let’s do some practice!

Wrap Up

Do Next

  1. Read Chapter 17: DateTimes from r4ds.
  2. Open the Recitation Gem and say “Provide me practice problems for Chapter 17” or work through some of the exercises in the text.
  3. That’s it for today! See you tomorrow!