Examples for 'lubridate::ymd_hms'


Parse date-times with *y*ear, *m*onth, and *d*ay, *h*our, *m*inute, and *s*econd components.

Aliases: ymd_hms ymd_hm ymd_h dmy_hms dmy_hm dmy_h mdy_hms mdy_hm mdy_h ydm_hms ydm_hm ydm_h

Keywords: POSIXt parse

### ** Examples


x <- c("2010-04-14-04-35-59", "2010-04-01-12-00-00")
ymd_hms(x)
[1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
x <- c("2011-12-31 12:59:59", "2010-01-01 12:00:00")
ymd_hms(x)
[1] "2011-12-31 12:59:59 UTC" "2010-01-01 12:00:00 UTC"
## ** heterogeneous formats **
x <- c(20100101120101, "2009-01-02 12-01-02", "2009.01.03 12:01:03",
       "2009-1-4 12-1-4",
       "2009-1, 5 12:1, 5",
       "200901-08 1201-08",
       "2009 arbitrary 1 non-decimal 6 chars 12 in between 1 !!! 6",
       "OR collapsed formats: 20090107 120107 (as long as prefixed with zeros)",
       "Automatic wday, Thu, detection, 10-01-10 10:01:10 and p format: AM",
       "Created on 10-01-11 at 10:01:11 PM")
ymd_hms(x)
 [1] "2010-01-01 12:01:01 UTC" "2009-01-02 12:01:02 UTC"
 [3] "2009-01-03 12:01:03 UTC" "2009-01-04 12:01:04 UTC"
 [5] "2009-01-05 12:01:05 UTC" "2009-01-08 12:01:08 UTC"
 [7] "2009-01-06 12:01:06 UTC" "2009-01-07 12:01:07 UTC"
 [9] "2010-01-10 10:01:10 UTC" "2010-01-11 22:01:11 UTC"
## ** fractional seconds **
op <- options(digits.secs=3)
dmy_hms("20/2/06 11:16:16.683")
[1] "2006-02-20 11:16:16.683 UTC"
options(op)

## ** different formats for ISO8601 timezone offset **
ymd_hms(c("2013-01-24 19:39:07.880-0600",
"2013-01-24 19:39:07.880", "2013-01-24 19:39:07.880-06:00",
"2013-01-24 19:39:07.880-06", "2013-01-24 19:39:07.880Z"))
[1] "2013-01-25 01:39:07 UTC" "2013-01-24 19:39:07 UTC"
[3] "2013-01-25 01:39:07 UTC" "2013-01-25 01:39:07 UTC"
[5] "2013-01-24 19:39:07 UTC"
## ** internationalization **
## Not run: 
##D x_RO <- "Ma 2012 august 14 11:28:30 "
##D   ymd_hms(x_RO, locale = "ro_RO.utf8")
## End(Not run)

## ** truncated time-dates **
x <- c("2011-12-31 12:59:59", "2010-01-01 12:11", "2010-01-01 12", "2010-01-01")
ymd_hms(x, truncated = 3)
[1] "2011-12-31 12:59:59 UTC" "2010-01-01 12:11:00 UTC"
[3] "2010-01-01 12:00:00 UTC" "2010-01-01 00:00:00 UTC"
x <- c("2011-12-31 12:59", "2010-01-01 12", "2010-01-01")
ymd_hm(x, truncated = 2)
[1] "2011-12-31 12:59:00 UTC" "2010-01-01 12:00:00 UTC"
[3] "2010-01-01 00:00:00 UTC"
## ** What lubridate might not handle **
## Extremely weird cases when one of the separators is "" and some of the
## formats are not in double digits might not be parsed correctly:
## Not run: 
##D ymd_hm("20100201 07-01", "20100201 07-1", "20100201 7-01")
## End(Not run)

[Package lubridate version 1.8.0 Index]