EVOLUTION-MANAGER
Edit File: as.Date.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Date Conversion Functions to and from Character</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for as.Date {base}"><tr><td>as.Date {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Date Conversion Functions to and from Character</h2> <h3>Description</h3> <p>Functions to convert between character representations and objects of class <code>"Date"</code> representing calendar dates. </p> <h3>Usage</h3> <pre> as.Date(x, ...) ## S3 method for class 'character' as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"), optional = FALSE, ...) ## S3 method for class 'numeric' as.Date(x, origin, ...) ## S3 method for class 'POSIXct' as.Date(x, tz = "UTC", ...) ## S3 method for class 'Date' format(x, ...) ## S3 method for class 'Date' as.character(x, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>an object to be converted.</p> </td></tr> <tr valign="top"><td><code>format</code></td> <td> <p><code><a href="character.html">character</a></code> string. If not specified, it will try <code>tryFormats</code> one by one on the first non-<code>NA</code> element, and give an error if none works. Otherwise, the processing is via <code><a href="strptime.html">strptime</a></code>.</p> </td></tr> <tr valign="top"><td><code>tryFormats</code></td> <td> <p><code><a href="character.html">character</a></code> vector of <code>format</code> strings to try if <code>format</code> is not specified.</p> </td></tr> <tr valign="top"><td><code>optional</code></td> <td> <p><code><a href="logical.html">logical</a></code> indicating to return <code>NA</code> (instead of signalling an error) if the format guessing does not succeed.</p> </td></tr> <tr valign="top"><td><code>origin</code></td> <td> <p>a <code>Date</code> object, or something which can be coerced by <code>as.Date(origin, ...)</code> to such an object.</p> </td></tr> <tr valign="top"><td><code>tz</code></td> <td> <p>a time zone name.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments to be passed from or to other methods, including <code>format</code> for <code>as.character</code> and <code>as.Date</code> methods.</p> </td></tr> </table> <h3>Details</h3> <p>The usual vector re-cycling rules are applied to <code>x</code> and <code>format</code> so the answer will be of length that of the longer of the vectors. </p> <p>Locale-specific conversions to and from character strings are used where appropriate and available. This affects the names of the days and months. </p> <p>The <code>as.Date</code> methods accept character strings, factors, logical <code>NA</code> and objects of classes <code>"<a href="DateTimeClasses.html">POSIXlt</a>"</code> and <code>"<a href="DateTimeClasses.html">POSIXct</a>"</code>. (The last is converted to days by ignoring the time after midnight in the representation of the time in specified time zone, default UTC.) Also objects of class <code>"date"</code> (from package <span class="pkg"><a href="../../date/html/as.date.html">date</a></span>) and <code>"dates"</code> (from package <span class="pkg"><a href="../../chron/html/chron.html">chron</a></span>). Character strings are processed as far as necessary for the format specified: any trailing characters are ignored. </p> <p><code>as.Date</code> will accept numeric data (the number of days since an epoch), but <em>only</em> if <code>origin</code> is supplied. </p> <p>The <code>format</code> and <code>as.character</code> methods ignore any fractional part of the date. </p> <h3>Value</h3> <p>The <code>format</code> and <code>as.character</code> methods return a character vector representing the date. <code>NA</code> dates are returned as <code>NA_character_</code>. </p> <p>The <code>as.Date</code> methods return an object of class <code>"<a href="Dates.html">Date</a>"</code>. </p> <h3>Conversion from other Systems</h3> <p>Most systems record dates internally as the number of days since some origin, but this is fraught with problems, including </p> <ul> <li><p> Is the origin day 0 or day 1? As the ‘Examples’ show, Excel manages to use both choices for its two date systems. </p> </li> <li><p> If the origin is far enough back, the designers may show their ignorance of calendar systems. For example, Excel's designer thought 1900 was a leap year (claiming to copy the error from earlier DOS spreadsheets), and Matlab's designer chose the non-existent date of ‘January 0, 0000’ (there is no such day), not specifying the calendar. (There is such a year in the ‘Gregorian’ calendar as used in ISO 8601:2004, but that does say that it is only to be used for years before 1582 with the agreement of the parties in information exchange.) </p> </li></ul> <p>The only safe procedure is to check the other systems values for known dates: reports on the Internet (including R-help) are more often wrong than right. </p> <h3>Note</h3> <p>The default formats follow the rules of the ISO 8601 international standard which expresses a day as <code>"2001-02-03"</code>. </p> <p>If the date string does not specify the date completely, the returned answer may be system-specific. The most common behaviour is to assume that a missing year, month or day is the current one. If it specifies a date incorrectly, reliable implementations will give an error and the date is reported as <code>NA</code>. Unfortunately some common implementations (such as <span class="samp">glibc</span>) are unreliable and guess at the intended meaning. </p> <p>Years before 1CE (aka 1AD) will probably not be handled correctly. </p> <h3>References</h3> <p>International Organization for Standardization (2004, 1988, 1997, ...) <em>ISO 8601. Data elements and interchange formats – Information interchange – Representation of dates and times.</em> For links to versions available on-line see (at the time of writing) <a href="http://www.qsl.net/g1smd/isopdf.htm">http://www.qsl.net/g1smd/isopdf.htm</a>. </p> <h3>See Also</h3> <p><a href="Dates.html">Date</a> for details of the date class; <code><a href="locales.html">locales</a></code> to query or set a locale. </p> <p>Your system's help pages on <code>strftime</code> and <code>strptime</code> to see how to specify their formats. Windows users will find no help page for <code>strptime</code>: code based on <span class="samp">glibc</span> is used (with corrections), so all the format specifiers described here are supported, but with no alternative number representation nor era available in any locale. </p> <h3>Examples</h3> <pre> ## locale-specific version of the date format(Sys.Date(), "%a %b %d") ## read in date info in format 'ddmmmyyyy' ## This will give NA(s) in some locales; setting the C locale ## as in the commented lines will overcome this on most systems. ## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C") x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960") z <- as.Date(x, "%d%b%Y") ## Sys.setlocale("LC_TIME", lct) z ## read in date/time info in format 'm/d/y' dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") as.Date(dates, "%m/%d/%y") ## date given as number of days since 1900-01-01 (a date in 1989) as.Date(32768, origin = "1900-01-01") ## Excel is said to use 1900-01-01 as day 1 (Windows default) or ## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel ## incorrectly treating 1900 as a leap year. ## So for dates (post-1901) from Windows Excel as.Date(35981, origin = "1899-12-30") # 1998-07-05 ## and Mac Excel as.Date(34519, origin = "1904-01-01") # 1998-07-05 ## (these values come from http://support.microsoft.com/kb/214330) ## Experiment shows that Matlab's origin is 719529 days before ours, ## (it takes the non-existent 0000-01-01 as day 1) ## so Matlab day 734373 can be imported as as.Date(734373, origin = "1970-01-01") - 719529 # 2010-08-23 ## (value from ## http://www.mathworks.de/de/help/matlab/matlab_prog/represent-date-and-times-in-MATLAB.html) ## Time zone effect z <- ISOdate(2010, 04, 13, c(0,12)) # midnight and midday UTC as.Date(z) # in UTC ## these time zone names are common as.Date(z, tz = "NZ") as.Date(z, tz = "HST") # Hawaii </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>