EDS 221 Day 8 PM

Dates and times


August 19th, 2026

Why dates are tricky


Dates look like plain text, but they aren’t — "01/02/2017" could mean January 2nd or February 1st depending on where you’re reading from.

library(tidyverse)
library(lubridate)

class("2017-01-31")
[1] "character"

A character string has no concept of “one day later.” A proper date type does.

Parsing dates: ymd(), mdy(), dmy()


lubridate’s parsing functions are named for the order the year, month, and day appear in your text — they all return the same kind of date.

ymd("2017-01-31")
[1] "2017-01-31"
mdy("January 31st, 2017")
[1] "2017-01-31"
dmy("31-Jan-2017")
[1] "2017-01-31"

Parsing datetimes: ymd_hms()


Add _hms, _hm, or _h to also capture a time of day. Unspecified time components default to zero.

ymd_hms("2017-01-31 20:11:59")
[1] "2017-01-31 20:11:59 UTC"
mdy_hm("01/31/2017 08:01")
[1] "2017-01-31 08:01:00 UTC"

Extracting components


Once something is a date, you can pull out any piece of it — useful for grouping and filtering.

release_date <- ymd("2026-08-19")

year(release_date)
[1] 2026
month(release_date, label = TRUE)
[1] Aug
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
wday(release_date, label = TRUE)
[1] Wed
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

Date arithmetic


Dates support arithmetic directly. days(), weeks(), and friends add a fixed span of calendar time.

start <- ymd("2026-08-19")

start + days(1)
[1] "2026-08-20"
start + weeks(2)
[1] "2026-09-02"
ymd("2026-12-25") - start
Time difference of 128 days

The last line returns a difftime — the number of days between two dates.

What to memorize


Dates and datetimes are a distinct type from text — parse early, and let lubridate handle the arithmetic.

Creating

  • ymd() (and permutations)
  • ymd_hms() (and permutations)

Components

  • year(), month(), day(), wday()
  • hour(), minute(), second()