Introduction
This is the fifth tutorial in the series Handling Date & Time in R. In this tutorial, we will learn about date and time formats.
Resources
Below are the links to all the resources related to this tutorial:
Date & Time Formats
After the timezones and daylight savings detour, let us get back on path and explore another important aspect, date & time formats. Although it is a good practice to adher to ISO 8601 format, not all date/time data will comply with it. In real world, date/time data may come in all types of weird formats. Below is a sample
Format |
---|
December 12, 2019 |
12th Dec, 2019 |
Dec 12th, 19 |
12-Dec-19 |
2019 December |
12.12.19 |
When the data is not in the default ISO 8601 format, we need to explicitly specify the format in R. We do this using conversion specifications. A conversion specification is introduced by %, usually followed by a single letter or O or E and then a single letter.
Conversion Specifications
Specification | Description | Example |
---|---|---|
%d
|
Day of the month (decimal number) | 12 |
%m
|
Month (decimal number) | 12 |
%b
|
Month (abbreviated) | Dec |
%B
|
Month (full name) | December |
%y
|
Year (2 digit) | 19 |
%Y
|
Year (4 digit) | 2019 |
%H | Hour | 8 |
%M | Minute | 5 |
%S | Second | 3 |
Time to work through a few examples. Let us say you are dealing with dates in
the format 19/12/12
. In this format, the year comes first followed by month
and the date; each separated by a slash (/
). The year consists of only 2
digits i.e. it does not include the century. Let us now map each component of
the date to the conversion specification table shown at the beginning.
Date | Specification |
---|---|
19 |
%y
|
12 |
%m
|
12 |
%d
|
Using the format argument, we will specify the conversion specification as a character vector i.e. enclosed in quotes.
as.Date("19/12/12", format = "%y/%m/%d")
## [1] "2019-12-12"
Another way in which the release data can be written is 2019-Dec-12
. We still
have the year followed by the month and the date but there are a few changes
here:
- the components are separated by a
-
instead of/
- year has 4 digits i.e. includes the century
- the month is specified using abbreviation instead of digits
Let us map the components to the format table:
Date | Specification |
---|---|
2019 |
%Y
|
Dec |
%b
|
12 |
%d
|
Let us specify the format for the date using the above mapping.
as.Date("2019-Dec-12", format = "%Y-%b-%d")
## [1] "2019-12-12"
In both the above examples, we have not dealt with time components. Let us
include the release time of R 3.6.2 in the next one i.e.
19/12/12 08:05:03
.
Date | Specification |
---|---|
19 |
%y
|
12 |
%m
|
12 |
%d
|
08 |
%H
|
05 |
%M
|
03 |
%S
|
Since we are dealing with time, we will use as.POSIXct()
instead of
as.Date()
.
as.POSIXct("19/12/12 08:05:03", tz = "UTC", format = "%y/%m/%d %H:%M:%S")
## [1] "2019-12-12 08:05:03 UTC"
In the below table, we look at some of the most widely used conversion
specifications. You can learn more about these specifications by running
?strptime
or help(strptime)
.
Specification | Description |
---|---|
%a
|
Abbreviated weekday |
%A
|
Full weekday |
%C
|
Century (00-99) |
%D
|
Same as %m/%d/%y
|
%e
|
Day of month [1 - 31] |
%F
|
Same as %Y-%m-%d
|
%h
|
Same as %b
|
%I
|
Hours as decimal [01 - 12] |
%j
|
Day of year [001 - 366] |
%R
|
Same as %H:%M
|
%t
|
Tab |
%T
|
Same as %H:%M:%S
|
%u
|
Weekday 1 - 7 |
%U
|
Week of year [00 - 53] |
%V
|
Week of year [01 - 53] |
%w
|
Weekday 0 - 6 |
%W
|
Week of year [00 - 53] |
We have included a lot of practice questions for you to explore the different date/time formats. The solutions are available in the Learning Management System as well as in our GitHub repo. Try them and let us know if you have any doubts.
Guess Format
guess_formats()
from lubridate is a very useful function. It will guess the
date/time format if you specify the order in which year, month, date, hour,
minute and second appear.
release_date_formats <- c("December 12th 2019",
"Dec 12th 19",
"dec 12 2019")
guess_formats(release_date_formats,
orders = "mdy",
print_matches = TRUE)
## Omdy mdy
## [1,] "December 12th 2019" "%Om %dth %Y" "%B %dth %Y"
## [2,] "Dec 12th 19" "%Om %dth %y" "%b %dth %y"
## [3,] "dec 12 2019" "%Om %d %Y" "%b %d %Y"
## Omdy Omdy Omdy mdy mdy
## "%Om %dth %Y" "%Om %dth %y" "%Om %d %Y" "%B %dth %Y" "%b %dth %y"
## mdy
## "%b %d %Y"
Your Turn
Below, we have specified July 5th, 2019
in different ways. Create the date using as.Date()
while specifying the correct format for each of them.
05.07.19
5-July 2019
July 5th, 2019
July 05, 2019
2019-July- 05
05/07/2019
07/05/2019
7/5/2019
07/5/19
2019-07-05
*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: support@rsquaredacademy.com