# Date & Time in R - Date Arithmetic

## Introduction

This is the third tutorial in the series Handling Date & Time in R. In this tutorial, we will learn about date arithmetic.

## Resources

Below are the links to all the resources related to this tutorial:

## Date Arithmetic

Time to do some arithmetic with the dates. Let us calculate the length of a course you have enrolled for (Become a Rock Star Data Scientist in 10 Days) by subtracting the course start date from the course end date.

``````course_start    <- as_date('2017-04-12')
course_end      <- as_date('2017-04-21')
course_duration <- course_end - course_start
course_duration
## Time difference of 9 days``````

### Shift Date

Time to shift the course dates. We can shift a date by days, weeks or months. Let us shift the course start date by:

• 2 days
• 3 weeks
• 1 year

``````course_start + days(2)
## [1] "2017-04-14"
course_start + weeks(3)
## [1] "2017-05-03"
course_start + years(1)
## [1] "2018-04-12"``````

### Case Study

#### Compute days to settle invoice

Let us estimate the number of days to settle the invoice by subtracting the date of invoice from the date of payment.

``````transact %>%
mutate(
days_to_pay = Payment - Invoice
)``````
``````## # A tibble: 2,466 x 4
##    Invoice    Due        Payment    days_to_pay
##    <date>     <date>     <date>     <drtn>
##  1 2013-01-02 2013-02-01 2013-01-15 13 days
##  2 2013-01-26 2013-02-25 2013-03-03 36 days
##  3 2013-07-03 2013-08-02 2013-07-08  5 days
##  4 2013-02-10 2013-03-12 2013-03-17 35 days
##  5 2012-10-25 2012-11-24 2012-11-28 34 days
##  6 2012-01-27 2012-02-26 2012-02-22 26 days
##  7 2013-08-13 2013-09-12 2013-09-09 27 days
##  8 2012-12-16 2013-01-15 2013-01-12 27 days
##  9 2012-05-14 2012-06-13 2012-07-01 48 days
## 10 2013-07-01 2013-07-31 2013-07-26 25 days
## # ... with 2,456 more rows``````

#### Compute days over due

How many of the invoices were settled post the due date? We can find this by:

• subtracting the due date from the payment date
• counting the number of rows where delay > 0
``````transact %>%
mutate(
delay = Payment - Due
) %>%
filter(delay > 0) %>%
count(delay)``````
``````## # A tibble: 36 x 2
##    delay       n
##  * <drtn>  <int>
##  1  1 days    61
##  2  2 days    65
##  3  3 days    51
##  4  4 days    62
##  5  5 days    69
##  6  6 days    56
##  7  7 days    55
##  8  8 days    49
##  9  9 days    38
## 10 10 days    33
## # ... with 26 more rows``````

• compute the length of a vacation which begins on `2020-04-19` and ends on `2020-04-25`
• recompute the length of the vacation after shifting the vacation start and end date by `10` days and `2` weeks
• compute the days to settle invoice and days overdue from the `receivables.csv` data set
• compute the length of employment (only for those employees who have been terminated) from the `hr-data.csv` data set (use date of hire and termination)