In this module, we will learn to work with date/time data in R using lubridate, an R package that makes it easy to work with dates and time. Let us begin by installing and loading the lubridate pacakge.

`lubridate::origin()`

```
now()
today()
am(now())
pm(now())
```

```
transact <- read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/transact.csv')
transact
```

The data set has 3 columns. All the dates are in the format (yyyy-mm-dd).

- Invoice: invoice date
- Due: due date
- Payment: payment date

We will use the functions in the lubridate package to answer a few questions we have about the transact data.

- extract date, month and year from Due
- compute the number of days to settle invoice
- compute days over due
- check if due year is a leap year
- check when due day in february is 29, whether it is a leap year
- how many invoices were settled within due date
- how many invoices are due in each quarter
- what is the average duration between invoice date and payment date

```
this_day <- as_date('2017-03-23')
day(this_day)
month(this_day)
year(this_day)
```

```
transact %>%
mutate(
due_day = day(Due),
due_month = month(Due),
due_year = year(Due)
)
```

```
course_start <- as_date('2017-04-12')
course_end <- as_date('2017-04-21')
course_duration <- course_end - course_start
course_duration
```

Compute days to settle invoice

```
transact %>%
mutate(
days_to_pay = Payment - Invoice
)
```

Compute days over due

```
transact %>%
mutate(
delay = Payment - Due
)
```

Is due year a leap year?

```
transact %>%
mutate(
due_year = year(Due),
is_leap = leap_year(due_year)
)
```

If due day is February 29, is it a leap year?

```
transact %>%
mutate(
due_day = day(Due),
due_month = month(Due),
due_year = year(Due),
is_leap = leap_year(due_year)
) %>%
select(-(Invoice), -(Payment)) %>%
filter(due_month == 2 & due_day == 29)
```

```
course_start + days(2)
course_start + weeks(1)
course_start + years(1)
```

`interval(course_start, course_end)`

```
course_interval <- interval(course_start, course_end)
course_interval %>%
int_shift(by = days(1))
course_interval %>%
int_shift(by = weeks(1))
course_interval %>%
int_shift(by = years(1))
```

```
vacation_start <- as_date('2017-04-19')
vacation_end <- as_date('2017-04-25')
vacation_interval <- interval(vacation_start, vacation_end)
int_overlaps(course_interval, vacation_interval)
```

How many invoices were settled within due date?

```
transact %>%
mutate(
inv_due_interval = interval(Invoice, Due),
due_next = Due + days(1),
due_pay_interval = interval(due_next, Payment),
overlaps = int_overlaps(inv_due_interval, due_pay_interval)
) %>%
select(Invoice, Due, Payment, overlaps)
```

How many invoices were settled within due date?

```
# using int_shift
transact %>%
mutate(
inv_due_interval = interval(Invoice, Due),
due_pay_interval = interval(Due, Payment),
due_pay_next = int_shift(due_pay_interval, by = days(1)),
overlaps = int_overlaps(inv_due_interval, due_pay_next)
) %>%
select(Invoice, Due, Payment, overlaps)
```

```
conference <- as_date('2017-04-15')
conference %within% interval(course_start, course_end)
```

How many invoices were settled within due date?

```
transact %>%
mutate(
inv_due_interval = interval(Invoice, Due),
overlaps = Payment %within% inv_due_interval
) %>%
select(Due, Payment, overlaps)
```

```
course_start
course_start %>%
quarter()
course_start %>%
quarter(with_year = TRUE)
```

```
transact %>%
mutate(
Quarter = quarter(Due)
)
```

```
transact %>%
mutate(
Quarter = quarter(Due, with_year = TRUE)
)
```

```
course_duration %>%
as.numeric() %>%
duration(units = 'days')
```

```
interval(course_start, course_end) / dseconds()
interval(course_start, course_end) / dminutes()
interval(course_start, course_end) / dhours()
interval(course_start, course_end) / dweeks()
interval(course_start, course_end) / dyears()
```

```
interval(course_start, course_end) %>%
time_length(unit = "seconds")
interval(course_start, course_end) %>%
time_length(unit = "minutes")
interval(course_start, course_end) %>%
time_length(unit = "hours")
```

```
interval(course_start, course_end) %>%
as.period(unit = "seconds")
interval(course_start, course_end) %>%
as.period(unit = "minutes")
interval(course_start, course_end) %>%
as.period(unit = "hours")
```