Skip to content

Date

Create

Action Code Details
From ymd
ISOdate(year, month, day) |> as.Date()
From ymd
lubridate::make_date(year, month, day)
Current date
Sys.Date()
Current date
lubridate::today() |> as.Date()
Current date in UTC timezone
lubridate::today(tzone = 'UTC') |> as.Date()
Unix epoch
as.Date(0)
From total days since Unix epoch
as.Date(n)
From total days since reference date
as.Date(n, origin = ref_date)
Date from YYYY-MM-DD string
as.Date(x)
Date from Y-m-d ordered string
lubridate::ymd(x)
Date from DD-MMM-YYYY string
as.Date(x, '%Y-%m-%d')
Date from d-m-Y ordered string
lubridate::dmy(x)

Vector of dates

Action Code Details
Vector of length n filled with date d
rep(d, n)
Sequence (vector) of n consecutive dates, starting from d
seq(d, length.out = n, by = 'day')
Sequence of n consecutive dates, starting from d
seq(d, length.out = n, by = 'day')
Sequence (vector) of consecutive dates between dates d and d2
seq(d, d2, by = 'day')
Sequence of n dates, starting from d with weekly increments
seq(d, length.out = n, by = 'week')
Sequence of n dates counting down from date d with weekly decrements
seq(d, length.out = n, by = '-1 week')
Sequence of n dates, starting from d with w-week increments
seq(d, length.out = n, by = 'w week')
Sequence of dates between d and d2 with weekly increment
seq(d, d2, by = 'week')
Sequence of n dates, starting from d with yearly increments
seq(d, length.out = n, by = 'year')

Constants

Action Code Details
NA
lubridate::NA_Date_

Test

Action Code Details
Is date
lubridate::is.Date(x)
Same date
x == y
Happens before y
x < y
Happens after y
x > y

Extract

Action Code Details
Days from Unix epoch
as.integer(x)
Seconds from Unix epoch (timestamp)
as.POSIXct(x) |> as.integer()
Year
lubridate::year(x)
Month
lubridate::month(x)
Day
lubridate::day(x)
Week number (1-53)
lubridate::week(x)
ISO week number (1-52)
lubridate::isoweek(x)
Weekday number
lubridate::wday(x, week_start = 1)
Mon=1, Sun=7

Update

Action Code Details
Change to first day of year
lubridate::yday(x) = 1
Change to first day of month
lubridate::day(x) = 1
Change year to y
lubridate::year(x) = y

Derive

Action Code Details
Change to first day of year
trunc(x, 'year')
Change to first day of month
trunc(d, 'month')
Shift forward by n days
x + n

Convert

Action Code Details
To datetime at midnight (00:00:00) in UTC
as.POSIXct(x)
To POSIXct at midnight (00:00:00) in UTC
as.POSIXct(x)
To POSIXlt at midnight (00:00:00) in UTC
as.POSIXlt(x)

Format

Action Code Details
Format as ISO date (yyyy-mm-dd)
format(x)
Format as dd-mm-yyyy
format(d, '%d-%m-%Y')
Format in system locale
format(x, '%a %b %d')