as.POSIX* {base} | R Documentation

## Date-time Conversion Functions

### Description

Functions to manipulate objects of classes `"POSIXlt"` and `"POSIXct"` representing calendar dates and times.

### Usage

    as.POSIXct(x, tz = "")
    as.POSIXlt(x, tz = "")

    ## S3 method for class 'POSIXlt':
    as.numeric(x)

### Arguments

`x` | An object to be converted.
`tz` | A timezone specification to be used for the conversion, if one is required. System-specific, but `""` is the current timezone, and `"GMT"` is UTC (Coordinated Universal Time, in French).

### Details

The `as.POSIX*` functions convert an object to one of the two classes used to represent date/times (calendar dates plus time to the nearest second). They can convert a wide variety of objects, including objects of the other class and of classes `"Date"`, `"date"` (from package date or survival), `"chron"` and `"dates"` (from package chron) to these classes. Dates are treated as being at midnight UTC.

They can also convert character strings of the formats `"2001-02-03"` and `"2001/02/03"` optionally followed by white space and a time in the format `"14:52"` or `"14:52:03"`. (Formats such as `"01/02/03"` are ambiguous but can be converted via a format specification by `strptime`.)

Logical `NA`s can be converted to either of the classes, but no other logical vectors can be.

The `as.numeric` method converts `"POSIXlt"` objects to `"POSIXct"`.

Where OSes describe their valid timezones can be obscure. The help for `tzset` (or `_tzset` on Windows) can be helpful, but it can also be inaccurate. There is a cumbersome POSIX specification, (listed under environment variable `TZ` at http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html), which is often at least partially supported, but there may be other more user-friendly ways to specify timezones. Windows documents a specification of the form `GST-1GDT`, but seems always to apply the US rules for changing to/from DST with such a specification. (There appears to be no documented way to apply timezones using other DST rules except to switch to the timezone, e.g. from the Control Panel.) So valid time zone values are of the forms `EST5EDT` for a timezone 5 hours west of GMT called `EST` when daylight saving is in force and `EDT` when it is not, `HST10` for ‘Hawaii-Aleutian Standard Time’ (which has no DST), and `CST-9:30` for the Northern Territory of Australia. The abbreviations have to be exactly 3 letters, and have no meaning other than a labels on the output.

### Value

`as.POSIXct` and `as.POSIXlt` return an object of the appropriate class. If `tz` was specified, `as.POSIXlt` will give an appropriate `"tzone"` attribute.

### Note

If you want to extract specific aspects of a time (such as the day of the week) just convert it to class `"POSIXlt"` and extract the relevant component(s) of the list, or if you want a character representation (such as a named day of the week) use `format.POSIXlt` or `format.POSIXct`.

If a timezone is needed and that specified is invalid on your system, what happens is system-specific but it will probably be ignored.

### See Also

DateTimeClasses for details of the classes; `strptime` for conversion to and from character representations.

### Examples

    (z <- Sys.time())             # the current date, as class "POSIXct"
    unclass(z)                    # a large integer
    floor(unclass(z)/86400)       # the number of days since 1970-01-01
    (z <- as.POSIXlt(Sys.time())) # the current date, as class "POSIXlt"
    unlist(unclass(z))            # a list shown as a named vector

    as.POSIXlt(Sys.time(), "GMT") # the current time in GMT
    as.POSIXlt(Sys.time(), "EST5EDT")  # the current time in New York
    as.POSIXlt(Sys.time(), "HST10")    # the current time in Hawaii
    as.POSIXlt(Sys.time(), "CST-9:30") # the current time in Darwin

[Package base version 2.3.1 Index]