Ranae Dietzel & Andee Kaplan
lubridate
Life before lubridate
was so hard.
We will not even venture down this road.
When you encounter non-lubridate date code, be strong.
xckd.com
Getting R to recognize a date is usually the first step
dates<-c("10/20/2016", "10/21/2016", "10/22/2016")
mdy(dates)
## [1] "2016-10-20" "2016-10-21" "2016-10-22"
Now we are good.
parse_date_time
parse_date_time("2014-09-24 15:23:10", orders="ymd hms")
## [1] "2014-09-24 15:23:10 UTC"
parse_date_time("09/24/2014 15-23-10", orders="mdy hms")
## [1] "2014-09-24 15:23:10 UTC"
parse_date_time("24 09 2014 15 23 10", orders="dmy hms")
## [1] "2014-09-24 15:23:10 UTC"
parse_date_time("24-09-14 15-23-10", orders="dmy hms")
## [1] "2014-09-24 15:23:10 UTC"
parse_date_time("Sep 24, 2014 15:23:10", orders="mdy hms")
## [1] "2014-09-24 15:23:10 UTC"
month("2016-10-21")
## [1] 10
day("2016-10-21")
## [1] 21
year("2016-10-21")
## [1] 2016
yday("2016-10-21")
## [1] 295
yday(mdy(dates))
## [1] 294 295 296
For example, when can Andee prepare for her prelim?
now<-Sys.time()
prelim<-"2016-10-25 09:00:00 CDT"
prep<-now %--% prelim
prep
## [1] 2016-10-21 14:58:35 UTC--2016-10-25 09:00:00 UTC
When will Ranae be at a Software Carpentry Instructor workshop?
depart<-"2016-10-23 12:00:00 CDT"
return<-"2016-10-25 20:00:00 CDT"
workshop<- depart %--% return
workshop
## [1] 2016-10-23 12:00:00 UTC--2016-10-25 20:00:00 UTC
Will Andee and Ranae be occupied at the same time?
int_overlaps(prep, workshop)
## [1] TRUE
When will Andee and Ranae both be unavailable?
setdiff(prep, workshop)
## [1] 2016-10-21 14:58:35 UTC--2016-10-23 12:00:00 UTC
The length of months and years change so often that doing arithmetic with them can be unintuitive. Consider a simple operation, January 31st + one month. Should the answer be
Lubridate does arithmetic with dates, but you still have to think about it and make decisions about what you really mean and want.
However, there are helpful functions such as:
How long does Andee have to prepare for her prelim?
as.period(prep %% months(1))
## [1] "3d 18H 1M 24.9760739803314S"