## S3 method for class 'Date': format(x, ...) ## S3 method for class 'Date': as.character(x, ...)## S3 method for class 'POSIXct': format(x, format = "", tz = "", usetz = FALSE, ...) ## S3 method for class 'POSIXlt': format(x, format = "", usetz = FALSE, ...) ## S3 method for class 'POSIXt': as.character(x, ...)
## S3 method for class 'difftime': format(x, ...)
strftime(x, format = "", tz = "", usetz = FALSE, ...)
| x | a Date, POSIXt, or difftime object. |
| format | a character vector specifying the output format. |
| tz | a character string specifying the time zone. |
| usetz | a logical value specifying if time zone should be appended to the output. |
| ... | additional arguments. |
| format.Date, as.character.Date | returns a character vector representing the Date object. |
| format.POSIXct, format.POSIXlt, as.character.POSIXt | returns a character vector representing the POSIXt object. |
| format.difftime | returns a character vector representing the difftime object. |
| strftime | returns a character vector representing the Date or POSIXt object. |
## format Date objects
x <- c("1970-01-01 00:00:00", "2038-01-19 03:14:07.123")
(d <- as.Date(x))
format(d)
format(d, format = "%m/%d/%Y", usetz = TRUE)
## format POSIXt objects
(pct <- as.POSIXct(x))
format(pct)
format(pct, tz = "Asia/Shanghai")
format(pct, format = "%Y-%m-%d %H:%M:%OS3", usetz = TRUE)
format(pct, format = "%a %b %d %H:%M:%S %Y")
# compare with date()
## format Date or POSIXt objects
strftime(d, format = "%m/%d/%Y", usetz = TRUE)
strftime(pct, format = "%Y-%m-%d %H:%M:%OS3",
tz = "Asia/Shanghai", usetz = TRUE)
## reformat date-time strings
strftime(x, format = "%a %b %d %H:%M:%S %Y", usetz = TRUE)
strftime(x, format = "%a %b %d %H:%M:%S %Y",
tz = "Asia/Shanghai", usetz = TRUE)
## option "digits.secs" for millisecond formatting
getOption("digits.secs")
format(pct)
ds.old <- options(digits.secs = 5)
format(pct)
options(ds.old)
## format difftime objects
(dt <- as.difftime(c("0:0:1", "0:1:0", "1:0:0")))
format(dt)