EVOLUTION-MANAGER
Edit File: parse_datetime.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: Parse date/times</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 parse_datetime {readr}"><tr><td>parse_datetime {readr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Parse date/times</h2> <h3>Description</h3> <p>Parse date/times </p> <h3>Usage</h3> <pre> parse_datetime(x, format = "", na = c("", "NA"), locale = default_locale(), trim_ws = TRUE) parse_date(x, format = "", na = c("", "NA"), locale = default_locale(), trim_ws = TRUE) parse_time(x, format = "", na = c("", "NA"), locale = default_locale(), trim_ws = TRUE) col_datetime(format = "") col_date(format = "") col_time(format = "") </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>A character vector of dates to parse.</p> </td></tr> <tr valign="top"><td><code>format</code></td> <td> <p>A format specification, as described below. If set to "", date times are parsed as ISO8601, dates and times used the date and time formats specified in the <code><a href="locale.html">locale()</a></code>. </p> <p>Unlike <code><a href="../../base/html/strptime.html">strptime()</a></code>, the format specification must match the complete string.</p> </td></tr> <tr valign="top"><td><code>na</code></td> <td> <p>Character vector of strings to interpret as missing values. Set this option to <code>character()</code> to indicate no missing values.</p> </td></tr> <tr valign="top"><td><code>locale</code></td> <td> <p>The locale controls defaults that vary from place to place. The default locale is US-centric (like R), but you can use <code><a href="locale.html">locale()</a></code> to create your own locale that controls things like the default time zone, encoding, decimal mark, big mark, and day/month names.</p> </td></tr> <tr valign="top"><td><code>trim_ws</code></td> <td> <p>Should leading and trailing whitespace be trimmed from each field before parsing it?</p> </td></tr> </table> <h3>Value</h3> <p>A <code><a href="../../base/html/DateTimeClasses.html">POSIXct()</a></code> vector with <code>tzone</code> attribute set to <code>tz</code>. Elements that could not be parsed (or did not generate valid dates) will be set to <code>NA</code>, and a warning message will inform you of the total number of failures. </p> <h3>Format specification</h3> <p><code>readr</code> uses a format specification similar to <code><a href="../../base/html/strptime.html">strptime()</a></code>. There are three types of element: </p> <ol> <li><p> Date components are specified with "%" followed by a letter. For example "%Y" matches a 4 digit year, "%m", matches a 2 digit month and "%d" matches a 2 digit day. Month and day default to <code>1</code>, (i.e. Jan 1st) if not present, for example if only a year is given. </p> </li> <li><p> Whitespace is any sequence of zero or more whitespace characters. </p> </li> <li><p> Any other character is matched exactly. </p> </li></ol> <p><code>parse_datetime()</code> recognises the following format specifications: </p> <ul> <li><p> Year: "%Y" (4 digits). "%y" (2 digits); 00-69 -> 2000-2069, 70-99 -> 1970-1999. </p> </li> <li><p> Month: "%m" (2 digits), "%b" (abbreviated name in current locale), "%B" (full name in current locale). </p> </li> <li><p> Day: "%d" (2 digits), "%e" (optional leading space), " </p> </li> <li><p> Hour: "%H" or "%I", use I (and not H) with AM/PM. </p> </li> <li><p> Minutes: "%M" </p> </li> <li><p> Seconds: "%S" (integer seconds), "%OS" (partial seconds) </p> </li> <li><p> Time zone: "%Z" (as name, e.g. "America/Chicago"), "%z" (as offset from UTC, e.g. "+0800") </p> </li> <li><p> AM/PM indicator: "%p". </p> </li> <li><p> Non-digits: "%." skips one non-digit character, "%+" skips one or more non-digit characters, "%*" skips any number of non-digits characters. </p> </li> <li><p> Automatic parsers: "%AD" parses with a flexible YMD parser, "%AT" parses with a flexible HMS parser. </p> </li> <li><p> Shortcuts: "%D" = "%m/%d/%y", "%F" = "%Y-%m-%d", "%R" = "%H:%M", "%T" = "%H:%M:%S", "%x" = "%y/%m/%d". </p> </li></ul> <h3>ISO8601 support</h3> <p>Currently, readr does not support all of ISO8601. Missing features: </p> <ul> <li><p> Week & weekday specifications, e.g. "2013-W05", "2013-W05-10" </p> </li> <li><p> Ordinal dates, e.g. "2013-095". </p> </li> <li><p> Using commas instead of a period for decimal separator </p> </li></ul> <p>The parser is also a little laxer than ISO8601: </p> <ul> <li><p> Dates and times can be separated with a space, not just T. </p> </li> <li><p> Mostly correct specifications like "2009-05-19 14:" and "200912-01" work. </p> </li></ul> <h3>See Also</h3> <p>Other parsers: <code><a href="col_skip.html">col_skip</a></code>, <code><a href="spec.html">cols_condense</a></code>, <code><a href="cols.html">cols</a></code>, <code><a href="parse_factor.html">parse_factor</a></code>, <code><a href="parse_guess.html">parse_guess</a></code>, <code><a href="parse_atomic.html">parse_logical</a></code>, <code><a href="parse_number.html">parse_number</a></code>, <code><a href="parse_vector.html">parse_vector</a></code> </p> <h3>Examples</h3> <pre> # Format strings -------------------------------------------------------- parse_datetime("01/02/2010", "%d/%m/%Y") parse_datetime("01/02/2010", "%m/%d/%Y") # Handle any separator parse_datetime("01/02/2010", "%m%.%d%.%Y") # Dates look the same, but internally they use the number of days since # 1970-01-01 instead of the number of seconds. This avoids a whole lot # of troubles related to time zones, so use if you can. parse_date("01/02/2010", "%d/%m/%Y") parse_date("01/02/2010", "%m/%d/%Y") # You can parse timezones from strings (as listed in OlsonNames()) parse_datetime("2010/01/01 12:00 US/Central", "%Y/%m/%d %H:%M %Z") # Or from offsets parse_datetime("2010/01/01 12:00 -0600", "%Y/%m/%d %H:%M %z") # Use the locale parameter to control the default time zone # (but note UTC is considerably faster than other options) parse_datetime("2010/01/01 12:00", "%Y/%m/%d %H:%M", locale = locale(tz = "US/Central")) parse_datetime("2010/01/01 12:00", "%Y/%m/%d %H:%M", locale = locale(tz = "US/Eastern")) # Unlike strptime, the format specification must match the complete # string (ignoring leading and trailing whitespace). This avoids common # errors: strptime("01/02/2010", "%d/%m/%y") parse_datetime("01/02/2010", "%d/%m/%y") # Failures ------------------------------------------------------------- parse_datetime("01/01/2010", "%d/%m/%Y") parse_datetime(c("01/ab/2010", "32/01/2010"), "%d/%m/%Y") # Locales -------------------------------------------------------------- # By default, readr expects English date/times, but that's easy to change' parse_datetime("1 janvier 2015", "%d %B %Y", locale = locale("fr")) parse_datetime("1 enero 2015", "%d %B %Y", locale = locale("es")) # ISO8601 -------------------------------------------------------------- # With separators parse_datetime("1979-10-14") parse_datetime("1979-10-14T10") parse_datetime("1979-10-14T10:11") parse_datetime("1979-10-14T10:11:12") parse_datetime("1979-10-14T10:11:12.12345") # Without separators parse_datetime("19791014") parse_datetime("19791014T101112") # Time zones us_central <- locale(tz = "US/Central") parse_datetime("1979-10-14T1010", locale = us_central) parse_datetime("1979-10-14T1010-0500", locale = us_central) parse_datetime("1979-10-14T1010Z", locale = us_central) # Your current time zone parse_datetime("1979-10-14T1010", locale = locale(tz = "")) </pre> <hr /><div style="text-align: center;">[Package <em>readr</em> version 1.3.1 <a href="00Index.html">Index</a>]</div> </body></html>