The objective of this tutorial is to explore several functions of the lubridate package to manipulate time and date objects.
df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
as.Date(df$Date, "%m/%d/%Y %H:%M:%S")
[1] "2009-10-09" "2009-10-15"
mytime<-ymd_hms("2015-08-14-05-30-00", tz="America/Halifax")
mytime
[1] "2015-08-14 05:30:00 ADT"
date(mytime)
[1] "2015-08-14"
Check for Leap Years in date
leap_year(mytime)
[1] FALSE
Check for Time Intervals
date1<-ymd_hms("2017-06-20-03-45-23")
date2<-ymd_hms("2017-10-07-21-02-19")
difftime(date2,date1)
Time difference of 109.7201 days
get current date or time
lubridate::today()
[1] "2017-08-28"
base::date()
[1] "Mon Aug 28 20:24:47 2017"
Sys.Date()
[1] "2017-08-28"
Sys.timezone()
[1] "America/Detroit"
now()
[1] "2017-08-28 20:24:47 EDT"
Extract day,month,year from date
year=year(today())
month=month(today())
day=day(today())
cbind.data.frame(year,month,day)
week <- wday(today())
month <- mday(today())
year <- yday(today())
cbind.data.frame(week,month,year)
week <- wday(today(),label=TRUE,abbr = FALSE)
#month <- mday(today(),label=TRUE,abbr = TRUE)
#year <- yday(today(),label=TRUE,abbr = TRUE)
cbind.data.frame(week,month,year)
formatting time
ymd(date(mytime))
[1] "2015-08-14"
ymd_hms(mytime)
[1] "2015-08-14 05:30:00 UTC"
mdy("March 6, 1957")
[1] "1957-03-06"
dmy("03/06/2015")
[1] "2015-06-03"
dmy(16082015)
[1] "2015-08-16"
ymd("1988/21/2")
All formats failed to parse. No formats found.
[1] NA
Parsing date-times
ymd_hms('2009-08-23 17:20:02')
[1] "2009-08-23 17:20:02 UTC"
hms("09:12:15")
[1] "9H 12M 15S"
hms(c('2014-05-14', '2014-09-22', '2014-07-11'))
[1] "2014H -5M -14S" "2014H -9M -22S" "2014H -7M -11S"
ymd_hms("2011-06-04 12:00:00", tz = "")
[1] "2011-06-04 12:00:00 EDT"
date <- as.POSIXct("01-01-2010",
format = "%d-%m-%Y", tz = "UTC")
date <- dmy("01-01-2010")
date time occur in the am or pm
x <- ymd("2012-03-26")
am(x)
[1] TRUE
pm(x)
[1] FALSE
mytime
[1] "2015-08-14 05:30:00 ADT"
am(mytime)
[1] TRUE
Change an object to an interval.
diff <- make_difftime(days = 31) #difftime
diff
Time difference of 31 days
as.interval(diff, ymd("2009-01-01"))
[1] 2009-01-01 UTC--2009-02-01 UTC
as.interval(diff, ymd("2009-02-01"))
[1] 2009-02-01 UTC--2009-03-04 UTC
Change an object to a duration.
x <- interval(ymd("2017-01-01"), ymd("2009-08-01")) #interval
x
[1] 2017-01-01 UTC--2009-08-01 UTC
as.duration(x)
[1] "234144000s (~7.42 years)"
as.duration(10) # numeric
[1] "10s"
dur <- duration(hours = 10, minutes = 6)
as.numeric(dur, "hours")
[1] 10.1
as.numeric(dur, "minutes")
[1] 606
Change an object to a period.
x <- interval(as.POSIXct("2009-01-01"), as.POSIXct("2010-02-02 01:01:01")) #interval
as.period(x)
[1] "1y 1m 1d 1H 1M 1S"
as.period(x, units = "day")
[1] "1y 1m 1d 1H 1M 1S"
leap <- interval(ymd("2016-01-01"), ymd("2017-01-01"))
as.period(leap, unit = "days")
[1] "366d 0H 0M 0S"
as.period(leap, unit = "years")
[1] "1y 0m 0d 0H 0M 0S"
dst <- interval(ymd("2016-11-06", tz = "America/Chicago"),
ymd("2016-11-07", tz = "America/Chicago"))
# as.period(dst, unit = "seconds")
as.period(dst, unit = "hours")
[1] "25H 0M 0S"
per <- period(hours = 10, minutes = 6)
as.numeric(per, "hours")
[1] 10.1
as.numeric(per, "minutes")
[1] 606
Convert an object to a date or date-time
## S4 method for signature 'numeric'
as_datetime(x, origin = lubridate::origin, tz = "UTC")
a vector of Date objects corresponding to x.
dt_utc <- ymd_hms("2010-08-03 00:50:50")
dt_europe <- ymd_hms("2010-08-03 00:50:50", tz="Europe/London")
c(as_date(dt_utc), as.Date(dt_utc))
c(as_date(dt_europe), as.Date(dt_europe))