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
Function
Description
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
Function
Description
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
Function
Description
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
Function
Description
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
Function
Description
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