Examples for 'lubridate::interval'


Utilities for creation and manipulation of 'Interval' objects

Aliases: interval %--% is.interval int_start int_start<- int_end int_end<- int_length int_flip int_shift int_overlaps int_standardize int_aligns int_diff

Keywords:

### ** Examples

interval(ymd(20090201), ymd(20090101))
[1] 2009-02-01 UTC--2009-01-01 UTC
date1 <- ymd_hms("2009-03-08 01:59:59")
date2 <- ymd_hms("2000-02-29 12:00:00")
interval(date2, date1)
[1] 2000-02-29 12:00:00 UTC--2009-03-08 01:59:59 UTC
interval(date1, date2)
[1] 2009-03-08 01:59:59 UTC--2000-02-29 12:00:00 UTC
span <- interval(ymd(20090101), ymd(20090201))

### ISO Intervals

interval("2007-03-01T13:00:00Z/2008-05-11T15:30:00Z")
[1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
[1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("P1Y2M10DT2H30M/2008-05-11T15:30:00Z")
[1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("2008-05-11/P2H30M")
[1] 2008-05-11 UTC--2010-11-11 02:00:00 UTC
### More permisive parsing (as long as there are no intermittent / characters)
interval("2008 05 11/P2hours 30minutes")
[1] 2008-05-11 UTC--2008-05-11 02:30:00 UTC
interval("08 05 11/P 2h 30m")
[1] 2008-05-11 UTC--2010-11-11 02:00:00 UTC
is.interval(period(months= 1, days = 15)) # FALSE
[1] FALSE
is.interval(interval(ymd(20090801), ymd(20090809))) # TRUE
[1] TRUE
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_start(int)
[1] "2001-01-01 UTC"
int_start(int) <- ymd("2001-06-01")
int
[1] 2001-06-01 UTC--2002-01-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_end(int)
[1] "2002-01-01 UTC"
int_end(int) <- ymd("2002-06-01")
int
[1] 2001-01-01 UTC--2002-06-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_length(int)
[1] 31536000
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_flip(int)
[1] 2002-01-01 UTC--2001-01-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_shift(int, duration(days = 11))
[1] 2001-01-12 UTC--2002-01-12 UTC
int_shift(int, duration(hours = -1))
[1] 2000-12-31 23:00:00 UTC--2001-12-31 23:00:00 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int2 <- interval(ymd("2001-06-01"), ymd("2002-06-01"))
int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01"))

int_overlaps(int1, int2) # TRUE
[1] TRUE
int_overlaps(int1, int3) # FALSE
[1] FALSE
int <- interval(ymd("2002-01-01"), ymd("2001-01-01"))
int_standardize(int)
[1] 2001-01-01 UTC--2002-01-01 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int2 <- interval(ymd("2001-06-01"), ymd("2002-01-01"))
int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01"))

int_aligns(int1, int2) # TRUE
[1] TRUE
int_aligns(int1, int3) # FALSE
[1] FALSE
dates <- now() + days(1:10)
int_diff(dates)
[1] 2025-08-12 17:03:31 UTC--2025-08-13 17:03:31 UTC
[2] 2025-08-13 17:03:31 UTC--2025-08-14 17:03:31 UTC
[3] 2025-08-14 17:03:31 UTC--2025-08-15 17:03:31 UTC
[4] 2025-08-15 17:03:31 UTC--2025-08-16 17:03:31 UTC
[5] 2025-08-16 17:03:31 UTC--2025-08-17 17:03:31 UTC
[6] 2025-08-17 17:03:31 UTC--2025-08-18 17:03:31 UTC
[7] 2025-08-18 17:03:31 UTC--2025-08-19 17:03:31 UTC
[8] 2025-08-19 17:03:31 UTC--2025-08-20 17:03:31 UTC
[9] 2025-08-20 17:03:31 UTC--2025-08-21 17:03:31 UTC

[Package lubridate version 1.8.0 Index]