Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this new series Handling Date & Time in R, we will learn to handle date & time in R. We will start off by learning how to get current date & time before moving on to understand how R handles date/time internally and the different classes such as Date & POSIXct/lt. We will spend some time exploring time zones, daylight savings and ISO 8001 standard for representing date/time. We will look at all the weird formats in which date/time come in real world and learn to parse them using conversion specifications. After this, we will also learn how to handle date/time columns while reading external data into R. We will learn to extract and update different date/time components such as year, month, day, hour, minute etc., create sequence of dates in different ways and explore intervals, durations and period. We will end the tutorial by learning how to round/rollback dates. Throughout the series, we will also work through a case study to better understand the concepts we learn. Happy learning!

## Resources

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

## Introduction

### Date

Let us begin by looking at the current date and time. Sys.Date() and today() will return the current date.

Sys.Date()
## [1] "2021-02-03"
lubridate::today()
## [1] "2021-02-03"

### Time

Sys.time() and now() return the date, time and the timezone. In now(), we can specify the timezone using the tzone argument.

Sys.time()
## [1] "2021-02-03 18:54:23 IST"
lubridate::now()
## [1] "2021-02-03 18:54:23 IST"
lubridate::now(tzone = "UTC")
## [1] "2021-02-03 13:24:23 UTC"

### AM or PM?

am() and pm() allow us to check whether date/time occur in the AM or PM? They return a logical value i.e. TRUE or FALSE

lubridate::am(now())
## [1] FALSE
lubridate::pm(now())
## [1] TRUE

### Leap Year

We can also check if the current year is a leap year using leap_year().

Sys.Date()
## [1] "2021-02-03"
lubridate::leap_year(Sys.Date())
## [1] FALSE

### Summary

Function Description
Sys.Date() Current Date
lubridate::today() Current Date
Sys.time() Current Time
lubridate::now() Current Time
lubridate::am() Whether time occurs in am?
lubridate::pm() Whether time occurs in pm?
lubridate::leap_year() Check if the year is a leap year?

• get current date
• get current time
• check whether the time occurs in am or pm?
• check whether the following years were leap years
• 2018
• 2016

## Case Study

Throughout the tutorial, we will work on a case study related to transactions of an imaginary trading company. The data set includes information about invoice and payment dates.

### Data

transact <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/transact.csv')
## # A tibble: 2,466 x 3
##    Invoice    Due        Payment
##
##  1 2013-01-02 2013-02-01 2013-01-15
##  2 2013-01-26 2013-02-25 2013-03-03
##  3 2013-07-03 2013-08-02 2013-07-08
##  4 2013-02-10 2013-03-12 2013-03-17
##  5 2012-10-25 2012-11-24 2012-11-28
##  6 2012-01-27 2012-02-26 2012-02-22
##  7 2013-08-13 2013-09-12 2013-09-09
##  8 2012-12-16 2013-01-15 2013-01-12
##  9 2012-05-14 2012-06-13 2012-07-01
## 10 2013-07-01 2013-07-31 2013-07-26
## # ... with 2,456 more rows

We will explore more about reading data sets with date/time columns after learning how to parse date/time. We have shared the code for reading the data sets used in the practice questions both in the Learning Management System as well as in our GitHub repo.

### Data Dictionary

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

Column Description
Invoice Invoice Date
Due Due Date
Payment Payment Date

In the case study, we will try 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

*As the reader of this blog, you are our most important critic and commentator. We value your opinion and want to know what we are doing right, what we could do better, what areas you would like to see us publish in, and any other words of wisdom you are willing to pass our way.

We welcome your comments. You can email to let us know what you did or did not like about our blog as well as what we can do to make our post better.*

Email: