Learn R Programming

⚠️There's a newer version (1.9.3) of this package.Take me there.

lubridate

Overview

Date-time data can be frustrating to work with in R. R commands for date-times are generally unintuitive and change depending on the type of date-time object being used. Moreover, the methods we use with date-times must be robust to time zones, leap days, daylight savings times, and other time related quirks, and R lacks these capabilities in some situations. Lubridate makes it easier to do the things R does with date-times and possible to do the things R does not.

If you are new to lubridate, the best place to start is the date and times chapter in R for data science.

Installation

# The easiest way to get lubridate is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just lubridate:
install.packages("lubridate")

# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/lubridate")

Cheatsheet

Features

library(lubridate, warn.conflicts = FALSE)
  • Easy and fast parsing of date-times: ymd(), ymd_hms, dmy(), dmy_hms, mdy(), …

    ymd(20101215)
    #> [1] "2010-12-15"
    mdy("4/1/17")
    #> [1] "2017-04-01"
  • Simple functions to get and set components of a date-time, such as year(), month(), mday(), hour(), minute() and second():

    bday <- dmy("14/10/1979")
    month(bday)
    #> [1] 10
    wday(bday, label = TRUE)
    #> [1] Sun
    #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
    
    year(bday) <- 2016
    wday(bday, label = TRUE)
    #> [1] Fri
    #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
  • Helper functions for handling time zones: with_tz(), force_tz()

    time <- ymd_hms("2010-12-13 15:30:30")
    time
    #> [1] "2010-12-13 15:30:30 UTC"
    
    # Changes printing
    with_tz(time, "America/Chicago")
    #> [1] "2010-12-13 09:30:30 CST"
    
    # Changes time
    force_tz(time, "America/Chicago")
    #> [1] "2010-12-13 15:30:30 CST"

Lubridate also expands the type of mathematical operations that can be performed with date-time objects. It introduces three new time span classes borrowed from https://www.joda.org.

  • durations, which measure the exact amount of time between two points

  • periods, which accurately track clock times despite leap years, leap seconds, and day light savings time

  • intervals, a protean summary of the time information between two points

Code of Conduct

Please note that the lubridate project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('lubridate')

Monthly Downloads

754,962

Version

1.8.0

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

October 7th, 2021

Functions in lubridate (1.8.0)

date_decimal

Converts a decimal to a date
is.Date

Various date utilities
force_tz

Replace time zone to create new date-time
format_ISO8601

Format in ISO8601 character format
DateCoercion

Convert a variety of date-time classes to POSIXlt and POSIXct
Timespan-class

Timespan class
make_datetime

Efficient creation of date-times from numeric representations
lubridate-package

Dates and times made easy with lubridate
Deprecated-lubridate

Deprecated functions in the lubridate package
stamp

Format dates and times based on human-friendly templates
DateTimeUpdate

Changes the components of a date object
am

Does date time occur in the am or pm?
as.interval

Change an object to an interval
time_length

Compute the exact length of a time span
as.duration

Change an object to a duration
guess_formats

Guess possible date-times formats from a character vector
format_ISO8601_precision_check

Provide a format for ISO8601 dates and times with the requested precision.
as_date

Convert an object to a date or date-time
as.period

Change an object to a period
Duration-class

Duration class
leap_year

Is a year a leap year?
decimal_date

Converts a date to a decimal of its year
local_time

Get local time from a date-time vector.
parse_date_time

User friendly date-time parsing functions
day

Get/set days component of a date-time
period

Create or parse period objects
timespan

Description of time span classes in lubridate
interval

Utilities for creation and manipulation of Interval objects
minute

Get/set minutes component of a date-time
make_difftime

Create a difftime object.
%within%

Does a date (or interval) fall within an interval?
hour

Get/set hours component of a date-time
days_in_month

Get the number of days in the month of a date-time
tz

Get/set time zone component of a date-time
is.POSIXt

Various POSIX utilities
period_to_seconds

Contrive a period to/from a given number of seconds
year

Get/set years component of a date-time
reclass_date

Convenience method to reclass dates post-modification.
origin

1970-01-01 UTC
now

The current day and time
dst

Get daylight savings time indicator of a date-time
fit_to_timeline

Fit a POSIXlt date-time to the timeline
is.timespan

Is x a length of time?
duration

Create a duration object.
lakers

Lakers 2008-2009 basketball data set
cyclic_encoding

Cyclic encoding of date-times
date

Get/set date component of a date-time
reclass_timespan

Convenience method to reclass timespans post-modification.
ymd

Parse dates with year, month, and day components
second

Get/set seconds component of a date-time
ymd_hms

Parse date-times with year, month, and day, hour, minute, and second components.
round_date

Round, floor and ceiling methods for date-time objects
hidden_aliases

Internal page for hidden aliases
quarter

Get the fiscal quarter and semester of a date-time
pretty_dates

Computes attractive axis breaks for date-time data
rollbackward

Roll backward or forward a date the previous, current or next month
ms

Parse periods with hour, minute, and second components
reexports

Objects exported from other packages
is.difftime

Is x a difftime object?
is.instant

Is x a date-time object?
%m+%

Add and subtract months to a date without exceeding the last day of the new month
with_tz

Get date-time in a different time zone
week

Get/set weeks component of a date-time
month

Get/set months component of a date-time
Interval-class

Interval class
Period-class

Period class