as.Date
Date-Time Parsing

Description

Functions for converting S objects (for example, character strings) to date-time objects.

Usage

as.Date(x, ...)
## Default S3 method:
as.Date(x, ...)
## S3 method for class 'numeric':
as.Date(x, origin, ...)
## S3 method for class 'character':
as.Date(x, format = "", ...)
## S3 method for class 'factor':
as.Date(x, ...)
## S3 method for class 'POSIXct':
as.Date(x, tz = "UTC", ...)
## S3 method for class 'POSIXlt':
as.Date(x, ...)
## S3 method for class 'date':
as.Date(x, ...)
## S3 method for class 'dates':
as.Date(x, ...)

as.POSIXct(x, tz = "", ...) ## Default S3 method: as.POSIXct(x, tz = "", ...) ## S3 method for class 'numeric': as.POSIXct(x, tz = "", origin, ...) ## S3 method for class 'Date': as.POSIXct(x, ...) ## S3 method for class 'factor': as.POSIXct(x, ...) ## S3 method for class 'POSIXlt': as.POSIXct(x, tz = "", ...) ## S3 method for class 'date': as.POSIXct(x, ...) ## S3 method for class 'dates': as.POSIXct(x, ...)

as.POSIXlt(x, tz = "", ...) ## Default S3 method: as.POSIXlt(x, tz = "", ...) ## S3 method for class 'numeric': as.POSIXlt(x, tz = "", origin, ...) ## S3 method for class 'character': as.POSIXlt(x, tz = "", format, ...) ## S3 method for class 'factor': as.POSIXlt(x, ...) ## S3 method for class 'Date': as.POSIXlt(x, ...) ## S3 method for class 'POSIXct': as.POSIXlt(x, tz = "", ...) ## S3 method for class 'date': as.POSIXlt(x, ...) ## S3 method for class 'dates': as.POSIXlt(x, ...)

strptime(x, format, tz = "")

as.difftime(tim, format = "%X", units = "auto")

Arguments

x any object to be converted to date-time.
format a character string specifying the date-time format; for example,
"%Y-%m-%d"
"%Y-%m-%d %H:%M"
"%Y-%m-%d %H:%M:%S"
"%Y-%m-%d %H:%M:%OS"
"%Y-%m-%d %H:%M:%OS3"
tz a character string specifying the time zone.
origin a character string specifying the starting date-time. Must be provided for the numeric methods.
tim any object to be converted to difftime. Usually character or numeric.
units a character string specifying the time unit from "auto", "secs", "mins", "hours", "days", and "weeks".
... additional arguments.

Details

The TIBCO Enterprise Runtime for R parsing routines are significantly faster when parsing date and time strings if the format argument only contains the control sequences %Y, %y, %m, %d, %H, %M, %S, or %OS. If the format argument contains other control sequences, or the string cannot be parsed, parsing can be much slower.
Value
as.Date.* return a Date object representing the date.
as.POSIXct.* return a POSIXct object representing the date-time.
as.POSIXlt.* return a POSIXlt object representing the date-time.
strptime return a POSIXlt object representing the date-time.
as.difftime returns a difftime object representing the time interval.
References
2004, 1988, 1997, ... ISO 8601. Data elements and interchange formats -- Information interchange -- Representation of dates and times. Geneva, Switzerland: International Organization for Standardization.
For links to versions available on-line see (at the time of writing) http://www.qsl.net/g1smd/isopdf.htm.
For information on the current official version, see (at the time of writing) http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html.
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/windows_tzid.html.
See Also
Date, POSIXt, format.Date, difftime
Examples
## convert to Date
as.Date(c("1900.01.01", "1970-01-01 00:00:00", "2038/01/19 03:14:08"))
as.Date("01/19/2038 03:14:08", format = "%m/%d/%Y %H:%M:%S")

as.Date(-1:1, origin = "1970-01-01") as.Date(-1:1, origin = "1970-01-01 00:00:00")

as.Date(Sys.time()) as.Date(Sys.time(), tz = "Asia/Shanghai")

## convert to POSIXct as.POSIXct(c("1900.01.01", "1970-01-01 00:00:00", "2038/01/19 03:14:08")) as.POSIXct("01/19/2038 03:14:08", format = "%m/%d/%Y %H:%M:%S", tz = "Asia/Shanghai")

as.POSIXct(-1:1, origin = "1970-01-01") as.POSIXct(-1:1, origin = "1970-01-01 00:00:00", tz = "Asia/Shanghai")

as.POSIXct(Sys.Date())

## convert to POSIXlt as.POSIXlt(c("1900.01.01", "1970-01-01 00:00:00", "2038/01/19 03:14:08")) as.POSIXlt("01/19/2038 03:14:08", format = "%m/%d/%Y %H:%M:%S", tz = "Asia/Shanghai")

as.POSIXlt(-1:1, origin = "1970-01-01") as.POSIXlt(-1:1, origin = "1970-01-01 00:00:00", tz = "Asia/Shanghai") as.POSIXlt(-1:1, origin = "1970-01-01 00:00:00", tz = "GMT")

as.POSIXlt(Sys.time()) as.POSIXlt(Sys.time(), tz = "Asia/Shanghai") as.POSIXlt(Sys.time(), tz = "GMT")

as.POSIXlt(Sys.Date())

## convert to POSIXlt with strptime strptime(c("1900.01.01", "1970-01-01 00:00:00", "2038/01/19 03:14:08"), format = "") strptime("01/19/2038 03:14:08", format = "%m/%d/%Y %H:%M:%S", tz = "Asia/Shanghai")

strptime(Sys.time(), format = "") strptime(Sys.time(), format = "%Y-%m-%d %H:%M:%S") strptime(Sys.Date(), format = "")

## parse strings and numerics into difftime objects as.difftime("0:1:0", format = "%H:%M:%S", units = "secs") as.difftime("1", format = "%M", units = "secs") as.difftime(1, units = "secs")

Package base version 6.0.0-69
Package Index