library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

Creating date/times

To get the current date or date-time

today()
## [1] "2021-01-24"
now()
## [1] "2021-01-24 23:54:54 EST"

Three ways to create a date/time:

  • From a string
  • From individual date-time components
  • From an existing date/time object

From a string

date_string = "2021-01-24"
class(date_string)
## [1] "character"
ymd(date_string)
## [1] "2021-01-24"
mdy("January 21st, 2021")
## [1] "2021-01-21"
dmy("24-January-2021")
## [1] "2021-01-24"

These functions also take unquoted numbers. This is the most concise way to create a single date/time object, as you might need when filtering date/time data. ymd() is short and unambiguous:

ymd(20210124)
## [1] "2021-01-24"
ymd(20210103)
## [1] "2021-01-03"
ydm(20210103)
## [1] "2021-03-01"

To create a date-time, add an underscore and one or more of “h”, “m”, and “s” to the name of the parsing function:

ymd_hms("2021-01-24 23:18:59")
## [1] "2021-01-24 23:18:59 UTC"
mdy_hm("01/24/2021 08:01")
## [1] "2021-01-24 08:01:00 UTC"

From individual components

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.0.5     v dplyr   1.0.3
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks stats::filter()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
library(nycflights13)
flights %>% 
  select(year, month, day, hour, minute) %>% 
  head(10)
## # A tibble: 10 x 5
##     year month   day  hour minute
##    <int> <int> <int> <dbl>  <dbl>
##  1  2013     1     1     5     15
##  2  2013     1     1     5     29
##  3  2013     1     1     5     40
##  4  2013     1     1     5     45
##  5  2013     1     1     6      0
##  6  2013     1     1     5     58
##  7  2013     1     1     6      0
##  8  2013     1     1     6      0
##  9  2013     1     1     6      0
## 10  2013     1     1     6      0

To create a date/time from this sort of input, use make_date() for dates, or make_datetime() for date-times:

flights %>% 
  select(year, month, day) %>% 
  mutate(departure = make_datetime(year, month, day))
## # A tibble: 336,776 x 4
##     year month   day departure          
##    <int> <int> <int> <dttm>             
##  1  2013     1     1 2013-01-01 00:00:00
##  2  2013     1     1 2013-01-01 00:00:00
##  3  2013     1     1 2013-01-01 00:00:00
##  4  2013     1     1 2013-01-01 00:00:00
##  5  2013     1     1 2013-01-01 00:00:00
##  6  2013     1     1 2013-01-01 00:00:00
##  7  2013     1     1 2013-01-01 00:00:00
##  8  2013     1     1 2013-01-01 00:00:00
##  9  2013     1     1 2013-01-01 00:00:00
## 10  2013     1     1 2013-01-01 00:00:00
## # ... with 336,766 more rows

From other types

to switch between a date-time and a date

as_datetime(today())
## [1] "2021-01-24 UTC"
as_date(now())
## [1] "2021-01-24"

Date-time components

Getting components

To pull out individual parts of the date with the accessor functions

  • year()

  • month()

  • mday() (day of the month)

  • yday() (day of the year)

  • wday() (day of the week)

  • hour()

  • minute()

  • second().