6 min read

Date & Time in R - Date & Time Components

Introduction

This is the seventh tutorial in the series Handling Date & Time in R. In this tutorial, we will learn to handle date & time components.

Resources

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

new courses ad

Date & Time Components

In the second section, we discussed the downside of saving date/time as character/string in R. One of the points we discussed was that we can’t extract components such as year, month, day etc. In this section, we will learn to extract date/time components such as

  • year
  • month
  • date
  • week
  • day
  • quarter
  • semester
  • hour
  • minute
  • second
  • timezone

The below table outlines the functions we will explore in the first part of this section.

Function Description
year() Get year
month() Get month (number)
month(label = TRUE) Get month (abbreviated name)
month(abbr = FALSE) Get month (full name)
months() Get month
week() Get week

Year

release_date <- ymd_hms("2019-12-12 08:05:03")
year(release_date) 
## [1] 2019

Month

month() will return the month as a number i.e. 12 for December.

month(release_date)
## [1] 12

Instead, if you want the name of the month , use the label argument and set it to TRUE. Now it returns Dec instead of 12.

month(release_date, label = TRUE)
## [1] Dec
## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec

But this is the abbreviated name and not the full name. How do we get the full name of the month? Set the abbr argument to FALSE.

month(release_date, label = TRUE, abbr  = FALSE)
## [1] December
## 12 Levels: January < February < March < April < May < June < ... < December

Ah! now we can see the full name of the month. months() from base R will return the full name of the month by default. If you want the abbreviated name, use the abbreviate argument and set it to TRUE.

months(release_date)
## [1] "December"

Week

week() returns the number of complete 7 day periods between the date and 1st January plus one.

week(release_date)
## [1] 50

Day

Use day() to extract the date component. There are other variations such as

Function Description
day Get day
mday() Day of the month
wday() Day of the week
qday() Day of quarter
yday() Day of year
weekdays() Day of week
days_in_month() Days in the month
day(release_date)
## [1] 12
mday(release_date)                 
## [1] 12
qday(release_date)                 
## [1] 73
yday(release_date)                 
## [1] 346

wday can return

  • a number
  • abbreviation of the weekday
  • full name of the weekday
wday(release_date)  
## [1] 5
wday(release_date, label = TRUE)
## [1] Thu
## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
wday(release_date, label = TRUE, abbr  = FALSE)  
## [1] Thursday
## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

weekdays() from base R also returns the day of the week (the name and not the number). If you want the abbreviated name, use the abbreviate argument.

weekdays(release_date)
## [1] "Thursday"
weekdays(release_date, abbreviate = TRUE)
## [1] "Thu"

Days in Month

If you want to know the number of days in the month, use days_in_month(). In our example, the month is December and it has 31 days.

days_in_month(release_date)
## Dec 
##  31

Hour, Minute & Seconds

Function Description
hour() Get hour
minute() Get minute
second() Get second
seconds() Number of seconds since 1970-01-01

So far we have been looking at date components. Now, let us look at time components.

hour(release_date)
## [1] 8
minute(release_date)
## [1] 5
second(release_date)
## [1] 3

seconds() returns the number of seconds since 1970-01-01.

seconds(release_date)
## [1] "1576137903S"

Quarter & Semester

quarter() will return the quarter from the date. December is in the 4th quarter and hence it returns 4.

quarter(release_date)
## [1] 4

If you want the year along with the quarter, set the with_year argument to TRUE.

quarter(release_date, with_year = TRUE)
## [1] 2019.4

In India, the fiscal starts in April and December falls in the 3rd quarter. How can we accommodate this change? The fiscal_start argument allows us to set the month in which the fiscal begins. We will set it to 4 for April. Now it returns 3 instead of 4.

quarter(release_date, fiscal_start = 4)    
## [1] 3

quarters() from base R also returns the quarter.

quarters(release_date)
## [1] "Q4"
Function Description
quarter() Get quarter
quarter(with_year = TRUE) Quarter with year
quarter(fiscal_start = 4) Fiscal starts in April
quarters() Get quarter
semester() Get semester

Case Study

Extract Date, Month & Year from Due Date

Let us now extract the date, month and year from the Due column.

transact %>%
  mutate(
    due_day   = day(Due),
    due_month = month(Due),
    due_year  = year(Due)
  )
## # A tibble: 2,466 x 6
##    Invoice    Due        Payment    due_day due_month due_year
##    <date>     <date>     <date>       <int>     <dbl>    <dbl>
##  1 2013-01-02 2013-02-01 2013-01-15       1         2     2013
##  2 2013-01-26 2013-02-25 2013-03-03      25         2     2013
##  3 2013-07-03 2013-08-02 2013-07-08       2         8     2013
##  4 2013-02-10 2013-03-12 2013-03-17      12         3     2013
##  5 2012-10-25 2012-11-24 2012-11-28      24        11     2012
##  6 2012-01-27 2012-02-26 2012-02-22      26         2     2012
##  7 2013-08-13 2013-09-12 2013-09-09      12         9     2013
##  8 2012-12-16 2013-01-15 2013-01-12      15         1     2013
##  9 2012-05-14 2012-06-13 2012-07-01      13         6     2012
## 10 2013-07-01 2013-07-31 2013-07-26      31         7     2013
## # ... with 2,456 more rows

Data Sanitization

Let us do some data sanitization. If the due day happens to be February 29, let us ensure that the due year is a leap year. Below are the steps to check if the due year is a leap year:

  • we will extract the following from the due date:
    • day
    • month
    • year
  • we will then create a new column is_leap which will have be set to TRUE if the year is a leap year else it will be set to FALSE
  • filter all the payments due on 29th Feb
  • select the following columns:
    • Due
    • is_leap
transact %>%
  mutate(
    due_day   = day(Due),
    due_month = month(Due),
    due_year  = year(Due),
    is_leap   = leap_year(due_year)
  ) %>%
  filter(due_month == 2 & due_day == 29) %>%
  select(Due, is_leap) 
## # A tibble: 4 x 2
##   Due        is_leap
##   <date>     <lgl>  
## 1 2012-02-29 TRUE   
## 2 2012-02-29 TRUE   
## 3 2012-02-29 TRUE   
## 4 2012-02-29 TRUE

Invoices Distribution by Quarter

Let us count the invoices due for each quarter.

transact %>%
  mutate(
    quarter_due = quarter(Due)
  ) %>%
  count(quarter_due)
## # A tibble: 4 x 2
##   quarter_due     n
## *       <int> <int>
## 1           1   521
## 2           2   661
## 3           3   618
## 4           4   666

Your Turn

Get the R release dates using r_versions() from the rversions package and tabulate the following

  • year
  • month with label
  • weekday with label
  • hour
  • and 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: