Skip to content

POSIXlt

Create

Action Code Details
From ymd
ISOdate(year, month, day) |> as.POSIXlt()
From ymd
lubridate::make_date(year, month, day) |> as.POSIXlt()
From ymdhms
ISOdatetime(year, month, day, hour, minute, second) |> as.POSIXlt()
From ymdhms in UTC timezone
ISOdatetime(year, month, day, hour, minute, second, tz = 'UTC') |> as.POSIXlt()
Current date in UTC
Sys.Date() |> as.POSIXlt()
Current datetime
Sys.time() |> as.POSIXlt()
Current datetime
lubridate::now() |> as.POSIXlt()
Current datetime in UTC timezone
lubridate::now(tzone = 'UTC') |> as.POSIXlt()
Unix epoch
as.POSIXlt(0)
From total seconds since Unix epoch
as.POSIXlt(n)
Datetime from YYYY-MM-DD string
as.POSIXlt(x)
Datetime from YYYY-MM-DD HH:mm:ss string
as.POSIXlt(x)
Date from DD-MM-YYYY string
as.POSIXlt(x, format = '%d-%m-%Y')

Test

Action Code Details
Is POSIXt
inherits(x, 'POSIXt')
Is POSIXt
lubridate::is.POSIXt(x)
Is POSIXlt
inherits(x, 'POSIXlt')
Is POSIXlt
lubridate::is.POSIXlt(x)
Same date
x == y
Happens before y
x < y
Happens after y
x > y

Extract

Action Code Details
Days from Unix epoch
as.Date(x) |> as.integer()
Seconds from Unix epoch (timestamp)
as.integer(x)
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
Hour
lubridate::hour(x)
Minute
lubridate::minute(x)
Second
lubridate::second(x)

Derive

Action Code Details
Shift forward by n seconds
x + n
Truncate to date
trunc(x, 'days')
Drops the time component, falling back to midnight 00:00:00
Truncate to hours
trunc(x, 'hours')
Truncate to minutes
trunc(x, 'mins')

Convert

Action Code Details
To Date
as.Date(x)
To POSIXct
as.POSIXct(x)

Format as string

Action Code Details
Format as ISO datetime (yyyy-mm-dd HH:MM:SS.SSS)
as.character(x)
Format as yyyy-mm-dd HH:MM:SS
format(x)
Format as yyyy-mm-dd
format(d, '%Y-%m-%d')
Format as dd-mm-yyyy
format(d, '%d-%m-%Y')
Format in system locale
format(x, '%a %b %d')