new_interval(date1, date2)
Intervals are time spans bound by two real date-times. Intervals can
be accurately converted to either period or duration objects using
as.period
, as.duration
. Since an interval
is anchored to a fixed history of time, both the number of seconds
that passed as well as the length of common time units during that
history can be calculated. Subtracting two date times automatically
creates an interval object. Intervals display as the difftime between
the two dates paired with the earlier, or beginning date.
interval
, as.interval
new_interval(ymd(20090101), ymd(20090201))
# 31 days beginning at 2009-01-01
date1 <- as.POSIXct("2009-03-08 01:59:59")
date2 <- as.POSIXct("2000-02-29 12:00:00")
new_interval(date1, date2)
# 3294.583 days beginning at 2000-02-29 12:00:00
new_interval(date2, date1)
# 3294.583 days beginning at 2000-02-29 12:00:00
span <- new_interval(ymd(20090101), ymd(20090201))
# 31 days beginning at 2009-01-01
span - days(30)
# 1 days beginning at 2009-01-01
span + months(6)
# 211.9583 days beginning at 2009-01-01
span$start
# "2009-01-01 CST"make
span$end
# "2009-02-01 CST"}
Run the code above in your browser using DataLab