Examples for 'base::as.POSIXct'


Date-time Conversion Functions

Aliases: as.POSIXct as.POSIXct.default as.POSIXct.POSIXlt as.POSIXct.date as.POSIXct.dates as.POSIXct.Date as.POSIXct.numeric as.POSIXlt as.POSIXlt.Date as.POSIXlt.date as.POSIXlt.dates as.POSIXlt.POSIXct as.POSIXlt.factor as.POSIXlt.character as.POSIXlt.default as.POSIXlt.numeric as.double.POSIXlt

Keywords: utilities chron

### ** Examples
## No test: 
(z <- Sys.time())             # the current datetime, as class "POSIXct"
[1] "2025-08-11 16:54:26 UTC"
unclass(z)                    # a large integer
[1] 1754931267
floor(unclass(z)/86400)       # the number of days since 1970-01-01 (UTC)
[1] 20311
(now <- as.POSIXlt(Sys.time())) # the current datetime, as class "POSIXlt"
[1] "2025-08-11 16:54:26 UTC"
unlist(unclass(now))          # a list shown as a named vector
               sec                min               hour               mday 
"26.9125437736511"               "54"               "16"               "11" 
               mon               year               wday               yday 
               "7"              "125"                "1"              "222" 
             isdst               zone             gmtoff 
               "0"              "UTC"                "0" 
now$year + 1900               # see ?DateTimeClasses
[1] 2025
months(now); weekdays(now)    # see ?months
[1] "August"
[1] "Monday"
## suppose we have a time in seconds since 1960-01-01 00:00:00 GMT
## (the origin used by SAS)
z <- 1472562988
# ways to convert this
as.POSIXct(z, origin = "1960-01-01")                # local
[1] "2006-08-30 13:16:28 UTC"
as.POSIXct(z, origin = "1960-01-01", tz = "GMT")    # in UTC
[1] "2006-08-30 13:16:28 GMT"
## SPSS dates (R-help 2006-02-16)
z <- c(10485849600, 10477641600, 10561104000, 10562745600)
as.Date(as.POSIXct(z, origin = "1582-10-14", tz = "GMT"))
[1] "1915-01-26" "1914-10-23" "1917-06-15" "1917-07-04"
## Stata date-times: milliseconds since 1960-01-01 00:00:00 GMT
## format %tc excludes leap-seconds, assumed here
## For format %tC including leap seconds, see foreign::read.dta()
z <- 1579598122120
op <- options(digits.secs = 3)
# avoid rounding down: milliseconds are not exactly representable
as.POSIXct((z+0.1)/1000, origin = "1960-01-01")
[1] "2010-01-20 09:15:22.120 UTC"
options(op)

## Matlab 'serial day number' (days and fractional days)
z <- 7.343736909722223e5 # 2010-08-23 16:35:00
as.POSIXct((z - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2010-08-23 16:35:00 UTC"
as.POSIXlt(Sys.time(), "GMT") # the current time in UTC
[1] "2025-08-11 16:54:26 GMT"
## End(No test)
## No test: 
## These may not be correct names on your system
as.POSIXlt(Sys.time(), "America/New_York")  # in New York
[1] "2025-08-11 12:54:26 EDT"
as.POSIXlt(Sys.time(), "EST5EDT")           # alternative.
[1] "2025-08-11 12:54:27 EDT"
as.POSIXlt(Sys.time(), "EST" )   # somewhere in Eastern Canada
[1] "2025-08-11 11:54:27 EST"
as.POSIXlt(Sys.time(), "HST")    # in Hawaii
[1] "2025-08-11 06:54:27 HST"
as.POSIXlt(Sys.time(), "Australia/Darwin")
[1] "2025-08-12 02:24:27 ACST"
## End(No test)

tab <- file.path(R.home("share"), "zoneinfo", "zone1970.tab")
if(file.exists(tab)) {
  cols <- c("code", "coordinates", "TZ", "comments")
  tmp <- read.delim(file.path(R.home("share"), "zoneinfo", "zone1970.tab"),
                    header = FALSE, comment.char = "#", col.names = cols)
  if(interactive()) View(tmp)
  head(tmp, 10)
}

[Package base version 4.2.3 Index]