Lubridate Cheat Sheet

In R

Lubridate is a popular R package that simplifies the process of working with dates and times. It provides a set of functions that make it easy to parse, manipulate, and format date-time objects in R.

It provides functions to convert between time zones and to handle ambiguous times that occur during daylight saving transitions. It can handle dates in formats such as “YYYY-MM-DD” or “MM/DD/YYYY”, and times in formats such as “HH:MM:SS” or “HH:MM”.

This cheat sheet provides an overview of the most commonly used functions in the lubridate package.

Date-Time Constructors

FunctionDescription
ymd()Create a date from year, month, and day
mdy()Create a date from month, day, and year
dmy()Create a date from day, month, and year
ymd_hms()Create a date-time from year, month, day, hour, minute, and second
ymd_hm()Create a date-time from year, month, day, hour, and minute
ymd_h()Create a date-time from year, month, day, and hour
ymd()Create a date from year, month, and day
today()Get today’s date
now()Get the current date and time

Date-Time Accessors

FunctionDescription
year()Get the year of a date-time
month()Get the month of a date-time
day()Get the day of a date-time
hour()Get the hour of a date-time
minute()Get the minute of a date-time
second()Get the second of a date-time
wday()Get the day of the week of a date-time
mday()Get the day of the month of a date-time
yday()Get the day of the year of a date-time
quarter()Get the quarter of a date-time
tz()Get the time zone of a date-time

Date-Time Arithmetic

FunctionDescription
seconds()Add or subtract seconds to a date-time
minutes()Add or subtract minutes to a date-time
hours()Add or subtract hours to a date-time
days()Add or subtract days to a date-time
weeks()Add or subtract weeks to a date-time
months()Add or subtract months to a date-time
years()Add or subtract years to a date-time

Date-Time Formatting

FunctionDescription
format()Format a date-time object
strftime()Format a date-time object using a C-style format string
parse_date_time()Parse a character string into a date-time object using a specified format string

Time Zones

FunctionDescription
with_tz()Set the time zone of a date-time object
force_tz()Set the time zone of a date-time object, even if it already has one
tz()Get the time zone of a date-time object
tz_set()Set the default time zone for the R session
tzones()List all available time zones

Durations

FunctionDescription
dseconds()Create a duration in seconds
dminutes()Create a duration in minutes
dhours()Create a duration in hours
ddays()Create a duration in days
dweeks()Create a duration in weeks
dmonths()Create a duration in months
dyears()Create a duration in years
duration()Create a duration from a numeric value and a unit
as.duration()Convert a time object to a duration object
as.period()Convert a duration object to a period object

Periods

FunctionDescription
years()Create a period of years
quarters()Create a period of quarters
months()Create a period of months
weeks()Create a period of weeks
days()Create a period of days
hours()Create a period of hours
minutes()Create a period of minutes
seconds()Create a period of seconds
period()Create a period from a numeric value and a unit
as.period()Convert a duration object to a period object
as.duration()Convert a period object to a duration object

References

  • lubridate documentation: https://lubridate.tidyverse.org/