Dates and Times I

Storing, creating, displaying, and converting time.

What are the results of the following operations?

t1 <- 2025-10-22+1
t2 <- 10/22-10/15
01:00

Agenda

  1. How does a computer Store dates and times?
  2. How do we Create dates and times?
  3. How do we Get and Set them?

Store

Unix Time

A method to measure time as the number of seconds1 that have elapsed since midnight UTC on 1 January 1970, the Unix epoch.

Examples:

Date Time (UTC) Unix Time
12:01 am Jan 1, 1970 60

Unix Time

A method to measure time as the number of seconds1 that have elapsed since midnight UTC on 1 January 1970, the Unix epoch.

Examples:

Date Time (UTC) Unix Time
12:01 am Jan 1, 1970 60
12:01 am Jan 2, 1970 86460

Unix Time

A method to measure time as the number of seconds1 that have elapsed since midnight UTC on 1 January 1970, the Unix epoch.

Examples:

Date Time (UTC) Unix Time
12:01 am Jan 1, 1970 60
12:01 am Jan 2, 1970 86460
7 pm Oct 22, 2025 (today at noon) 1761159600

Unix Time

A method to measure time as the number of seconds1 that have elapsed since midnight UTC on 1 January 1970, the Unix epoch.

Examples:

Date Time (UTC) Unix Time
12:01 am Jan 1, 1970 60
12:01 am Jan 2, 1970 86460
7 pm Oct 22, 2025 (today at noon) 1761159600
5:55 pm Dec 7, 1941 −8993325700

https://www.unixtimestamp.com/

An R vector for date-times

POSIXct (Portable Operating System Interface Calendar Time) is an atomic vector to store date-times that is built on top of a double vector.

now <- Sys.time()
now
[1] "2025-12-11 22:40:25 UTC"
class(now)
[1] "POSIXct" "POSIXt" 

Why not just use a double vector?

# it prints as a legible string
now
[1] "2025-12-11 22:40:25 UTC"
# while respecting artithmetic
now + 1
[1] "2025-12-11 22:40:26 UTC"
# and other sensible operations
round(now)
[1] "2025-12-11 22:40:25 UTC"
# and makes it easy to use other formats
format(now, "%m/%d/%y")
[1] "12/11/25"

Creating

Creating date-time vectors

Use as.POSIXct() to create a date-time vector from a numeric (in Unix Time) or a string in a particular format.

Creating date-time vectors

Use as.POSIXct() to create a date-time vector from a numeric (in Unix Time) or a string in a particular format.

as.POSIXct(x = 60, tz = "UTC")
[1] "1970-01-01 00:01:00 UTC"
as.POSIXct(x = "1970-01-01 00:01:00", tz = "UTC")
[1] "1970-01-01 00:01:00 UTC"
as.POSIXct(x = "Jan 1, 1970 12:01 am", 
           format = "%b %d, %Y %I:%M %p",  
           tz = "UTC")
[1] "1970-01-01 00:01:00 UTC"

Creating with lubridate

as.POSIXct(x = "1970-01-01 00:01:00", tz = "UTC")
[1] "1970-01-01 00:01:00 UTC"

lubridate() has many functions that can parse the year (y), month (m), day (d), hour (h), minute (m), and second (s) components of a date-time string.

library(lubridate)
mdy_hm("Jan 1, 1970 12:01 am")
[1] "1970-01-01 00:01:00 UTC"
dmy_hm("1-1-1970 00:01")
[1] "1970-01-01 00:01:00 UTC"
x <- dmy_hm("1-1-1970 00:01")
class(x)
[1] "POSIXct" "POSIXt" 

Getting and Setting

Getting

You can extract components of the date-time object using year(), month(), week(), day(), wday(), hour(), etc.

year(now)
[1] 2025
day(now)
[1] 11
wday(now)
[1] 5

Getting

You can extract components of the date-time object using year(), month(), week(), day(), wday(), hour(), etc.

year(now)
[1] 2025
day(now)
[1] 11
wday(now, label = TRUE)
[1] Thu
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

Getting

You can extract components of the date-time object using year(), month(), week(), day(), wday(), hour(), etc.

year(now)
[1] 2025
day(now)
[1] 11
wday(now, label = TRUE, abbr = FALSE)
[1] Thursday
7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Setting

You can set those same components in an existing date-time object using change-in-place notation.

year(now) <- 2026
now
[1] "2026-12-11 22:40:25 UTC"
wday(now, label = TRUE, abbr = FALSE)
[1] Friday
7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Your Turn

Question: Write the (lines) of code necessary to determine:

  1. On which day of the week will New Years Eve fall this year?
  2. What day of the week was US Independence Day?
  3. Bonus What was the corresponding Unix time?
02:00

Example: Circular Data

What is the average month of these observations?

# A tibble: 6 × 2
  month  year
  <dbl> <dbl>
1     2  2025
2     1  2025
3     2  2025
4    11  2024
5    12  2024
6    12  2024
df |>
  summarize(avg_month = mean(month))
# A tibble: 1 × 1
  avg_month
      <dbl>
1      6.67

Example: Circular Data

What is the month of the average of these dates?

df |>
  mutate(datetime = make_datetime(year = year,
                                  day = 15, 
                                  month = month))
# A tibble: 6 × 3
  month  year datetime           
  <dbl> <dbl> <dttm>             
1     2  2025 2025-02-15 00:00:00
2     1  2025 2025-01-15 00:00:00
3     2  2025 2025-02-15 00:00:00
4    11  2024 2024-11-15 00:00:00
5    12  2024 2024-12-15 00:00:00
6    12  2024 2024-12-15 00:00:00

Example: Circular Data

What is the month of the average of these dates?

df |>
  mutate(datetime = make_datetime(year = year,
                                  day = 15, 
                                  month = month)) |>
  summarize(avg_datetime = mean(datetime))
# A tibble: 1 × 1
  avg_datetime       
  <dttm>             
1 2025-01-04 20:00:00

Example: Circular Data

What is the month of the average of these dates?

df |>
  mutate(datetime = make_datetime(year = year,
                                  day = 15, 
                                  month = month)) |>
  summarize(avg_datetime = mean(datetime)) |>
  mutate(mon_of_avg = month(avg_datetime))
# A tibble: 1 × 2
  avg_datetime        mon_of_avg
  <dttm>                   <dbl>
1 2025-01-04 20:00:00          1