# Get the current date
today()[1] "2026-04-06"
Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 12c
Parse strings and build dates from individual components
Extract, round, and modify date-time components
Understand periods, durations, and intervals for time spans
Question: Why not just use strings for dates?
Dates and Times are special data types that understand the rules of the calendar and the clock.
Unlike strings, they know that leap years exist, how many days are in each month, and how to do math with time.
Question: How can I get the current date or time in R?
Question: How do I convert a character string into a proper Date object?
We can build them from strings using the matching {lubridate} function.
Question: What if my string also includes the time?
Sometimes your data has the date components spread across multiple columns. We will use the flights dataset to see this in action.
# A tibble: 336,776 × 6
year month day hour minute dep_delay
<int> <int> <int> <dbl> <dbl> <dbl>
1 2013 1 1 5 15 2
2 2013 1 1 5 29 4
3 2013 1 1 5 40 2
4 2013 1 1 5 45 -1
5 2013 1 1 6 0 -6
6 2013 1 1 5 58 -4
# ℹ 336,770 more rows
Question: How do I combine separate columns into one date variable?
# A tibble: 336,776 × 8
year month day hour minute dep_delay date datetime
<int> <int> <int> <dbl> <dbl> <dbl> <date> <dttm>
1 2013 1 1 5 15 2 2013-01-01 2013-01-01 05:15:00
2 2013 1 1 5 29 4 2013-01-01 2013-01-01 05:29:00
3 2013 1 1 5 40 2 2013-01-01 2013-01-01 05:40:00
4 2013 1 1 5 45 -1 2013-01-01 2013-01-01 05:45:00
5 2013 1 1 6 0 -6 2013-01-01 2013-01-01 06:00:00
# ℹ 336,771 more rows
Question: How do I get just the month or day out of a date-time?
Question: Can I get the actual name of the month or weekday?
Yes, set label = TRUE to return a factor instead of a number.
[1] Jul
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
Extracting components is incredibly useful for summaries and plots.
# A tibble: 7 × 2
weekday m_delay
<ord> <dbl>
1 Sat 7.65
2 Tue 10.6
3 Sun 11.6
4 Wed 11.8
5 Fri 14.7
6 Mon 14.8
7 Thu 16.1
Question: How can I group dates together by week or month?
Rounding dates allows us to aggregate and plot data over time. Notice that the x-axis includes the year, which is redundant since it’s always 2013!
Question: How can I format the date labels on a plot’s axis?
Use scale_x_date() with the date_labels argument. You can use special % codes to format the dates (e.g., %b for abbreviated month names).
When formatting dates for plots or parsing tricky strings, you will use special % codes to represent different calendar components.
| Code | Meaning | Example |
|---|---|---|
%Y |
4-digit year | “2026” |
%y |
2-digit year | “26” |
%m |
Month as a number | “04” |
%B |
Full month name | “April” |
%b |
Abbreviated month | “Apr” |
%d |
Day of the month | “06” |
When doing math with time, R provides three different classes of time spans. Picking the right one depends on what you are trying to calculate.
Periods use human logic, e.g., if you add one month to Jan 15, you get Feb 15th
Durations use strict math. They are always tracked in exact seconds. A duration year is exactly 365.25 days of seconds.
[1] "604800s (~1 weeks)"
[1] "31557600s (~1 years)"
Question: Why do we need both Periods and Durations?
Because human clocks are messy! Consider Daylight Saving Time.
Question: How do I calculate the exact time span between two dates?
An interval represents a span of time between two specific points. R handles all the complex leaps and bounds of the calendar for you.
[1] 1987-05-30 UTC--2026-04-06 UTC
You can divide an interval by different units depending on what you want.
Question: What if I just have a time, not a date?
The {hms} package is great for parsing and doing math with standalone times.
Time difference of 1298 secs