Date-time Conversion Functions

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

utilities, chron
as.POSIXct(x, tz = "", …)
as.POSIXlt(x, tz = "", …)

# S3 method for character as.POSIXlt(x, tz = "", format, …)

# S3 method for numeric as.POSIXlt(x, tz = "", origin, …)

# S3 method for POSIXlt as.double(x, …)


An object to be converted.


A time zone specification to be used for the conversion, if one is required. System-specific (see time zones), but "" is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.

further arguments to be passed to or from other methods.


character string giving a date-time format as used by strptime.


a date-time object, or something which can be coerced by as.POSIXct(tz = "GMT") to such an object.


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), "chron" and "dates" (from package chron) to these classes. Dates without times 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.) Fractional seconds are allowed. Alternatively, format can be specified for character vectors or factors: if it is not specified and no standard format works for all non-NA inputs an error is thrown.

If format is specified, remember that some of the format specifications are locale-specific, and you may need to set the LC_TIME category appropriately via Sys.setlocale. This most often affects the use of %b, %B (month names) and %p (AM/PM).

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

If you are given a numeric time as the number of seconds since an epoch, see the examples.

Character input is first converted to class "POSIXlt" by strptime: numeric input is first converted to "POSIXct". Any conversion that needs to go between the two date-time classes requires a time zone: conversion from "POSIXlt" to "POSIXct" will validate times in the selected time zone. One issue is what happens at transitions to and from DST, for example in the UK

as.POSIXct(strptime("2011-03-27 01:30:00", "%Y-%m-%d %H:%M:%S"))
as.POSIXct(strptime("2010-10-31 01:30:00", "%Y-%m-%d %H:%M:%S"))

are respectively invalid (the clocks went forward at 1:00 GMT to 2:00 BST) and ambiguous (the clocks went back at 2:00 BST to 1:00 GMT). What happens in such cases is OS-specific: one should expect the first to be NA, but the second could be interpreted as either BST or GMT (and common OSes give both possible values). Note too (see strftime) that OS facilities may not format invalid times correctly.


as.POSIXct and as.POSIXlt return an object of the appropriate class. If tz was specified, as.POSIXlt will give an appropriate "tzone" attribute. Date-times known to be invalid will be returned as NA.


Some of the concepts used have to be extended backwards in time (the usage is said to be ‘proleptic’). For example, the origin of time for the "POSIXct" class, ‘1970-01-01 00:00.00 UTC’, is before UTC was defined. More importantly, conversion is done assuming the Gregorian calendar which was introduced in 1582 and not used universally until the 20th century. One of the re-interpretations assumed by ISO 8601:2004 is that there was a year zero, even though current year numbering (and zero) is a much later concept (525 AD for year numbers from 1 AD).

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 the format method.

If a time zone is needed and that specified is invalid on your system, what happens is system-specific but attempts to set it will probably be ignored.

Conversion from character needs to find a suitable format unless one is supplied (by trying common formats in turn): this can be slow for long inputs.

See Also

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

Sys.timezone for details of the (system-specific) naming of time zones.

locales for locale-specific aspects.

  • as.POSIXct
  • as.POSIXct.default
  • as.POSIXct.POSIXlt
  • as.POSIXct.dates
  • as.POSIXct.Date
  • as.POSIXct.numeric
  • as.POSIXlt
  • as.POSIXlt.Date
  • as.POSIXlt.dates
  • as.POSIXlt.POSIXct
  • as.POSIXlt.factor
  • as.POSIXlt.character
  • as.POSIXlt.default
  • as.POSIXlt.numeric
  • as.double.POSIXlt
library(base) ## These may not be correct names on your system as.POSIXlt(Sys.time(), "America/New_York") # in New York as.POSIXlt(Sys.time(), "EST5EDT") # alternative. as.POSIXlt(Sys.time(), "EST" ) # somewhere in Eastern Canada as.POSIXlt(Sys.time(), "HST") # in Hawaii as.POSIXlt(Sys.time(), "Australia/Darwin") cols <- c("code", "coordinates", "TZ", "comments") tmp <- read.delim(file.path(R.home("share"), "zoneinfo", ""), header = FALSE, comment.char = "#", col.names = cols) if(interactive()) View(tmp)
Documentation reproduced from package base, version 3.4.1, License: Part of R 3.4.1

Community examples at Nov 25, 2016 base v3.3.1

## example * Using system time * Create and Compare 3 objects from system time ```r now<-Sys.Date() d1<- as.Date(now) class(d1) type(d1) d2<-as.POSIXct(now) d3<-as.POSIXlt(now) ```