Here we’ll review the creation of dates and times, the components that make up dates and times, time spans, and time zones in R.1 See the RStudio Dates and Times Cheat Sheet for a condensed reference.
There are three types of data that refer to an instant in time: dates, times, and date-times. You can retrieve the current date with
date() and the current date-time with
We will use the following three packages in this post:
Creating Dates and Times
1. Create dates and times from strings
Use a function containing
y, m, d and
h, m, s to turn a string or series of numbers into a date or date-time.
2. Create dates and times from other types of data
as_time() to convert an object to a date or a date-time.
3. Create dates and times from components spread across multiple columns in a table
make_datetime() to create a date-time if your data is spread out across multiple columns in a table. For example, in the nycflights13 dataset, there are separate columns for year, month, day, departure hour, and departure minute. We can create a new date-time column for departure time, and use this new column to visualize the distribution of flights on any day.
Retrieve and Edit Date and Time Components
Retrieve date and time components from a date or date-time with the following functions:
year()returns the year
month(..., label = TRUE)returns the month; set label = TRUE to return abbreviated month name
wday(..., label = TRUE, abbr = FALSE)returns the day of the week; set abbr = FALSE to return full name
mday()returns the day of the month
yday()returns the day of the year
hour()returns the hour
minute()returns the minute
secondreturns the second
Edit date and time components with the following functions:
update(<DATE>, year = ..., month = ..., mday = ..., hour = ...)updates dates manually
round_date()rounds to the nearest specified unit (minute, hour, day, week, month, year, etc.)
floor_date()rounds down to the nearest specified unit
ceiling_date()rounds up to the nearest specified unit
Tip: Set larger components of a date to a constant to explore patterns in the smaller components. For example, in nycflights13, we can set the flight day to a constant to view the distribution of flights across the course of the day for every day of the year:
Time Spans: Durations, Periods and Intervals
If you subtract two dates in R regularly, you get a “difftime” object that records a time span in seconds, minutes, hours, days, or weeks, which isn’t great because the units aren’t consistent. Time spans are better measured in these three ways:
- Durations, which represent an exact number of seconds
- Periods, which represent human units of time, i.e. weeks, months, and years
- Intervals, which are durations with a starting and ending point
Durations measure time in seconds. They can be added to each other, multiplied by a constant, and added or subtracted from dates and times.
Note: When adding or subtracting durations to dates, be aware of time discrepancies such as daylight savings time.
Periods measure time in human units, such as weeks and months. This takes into account time discrepancies such as daylight savings time with adding or subtracting from dates or times.
Intervals are durations with starting an ending points, and are represented by
%--%. They are useful when trying to determine how long a specific period is, sice you want to include all time discrepancies for that period.
For example, you may want to know the number of days between today and the same day next year – this calculation depends on whether it’s a leap year or not, so we should use intervals.
Time zones are complicated, but here are a couple of basic functions that are useful to know:
Sys.timezone()returns the time zone of your workstation, according to R
with_tz(<DATETIME>, tzone = "America/Detroit")changes time zone while keeping the instant in time
force_tz(<DATETIME>, tzone = "America/Detroit")changes time zone and changes the instant in time