Hi, In this tutorial, I will explain the use of lubridate package and how to use the lubridate package in R Programming.

Converting string to date is always a tedious task and more painful for developers if you don't know the date formats of R Programming. You can use as.Date() function to convert the string to date. However, the input string which is given to as.Date() function must be  in the below format

2019-01-20
Year-Month-Date format

If your input string is not in the above format, then the as.Date() function will throw error. So you have to mention the format of the date using the format parameter.

This work can be simplified using the lubridate package.

Install Lubridate Package:

You can install the lubridate package using the below command.

install.packages("lubridate")

Use Lubridate:

To use lubridate package, you must import the package

library(lubridate)

PARSE DATE-TIMES (Convert strings or numbers to date-times)

1. Identify the order of the year (y), month (m), day (d), hour (h),
minute (m) and second (s) elements in your data.
2. Use the function below whose name replicates the order. Each
accepts a wide variety of input formats.

Let's take Oracle database default date format

20-JAN-19

If you want to convert this format to R date format using lubridate, use below code.

dmy("20-JAN-19")

Output:
"2019-01-20"

 

dmy is a function which will look for date, month and year in a string and will convert it to date.

You can check by adding days to the dmy function.

dmy("20-JAN-19")+3

Output:
"2019-01-23"

So it added the 3 days. It works perfectly. Let's check the different format.


 

> ymd_hms("2017-11-28T14:02:00")
[1] "2017-11-28 14:02:00 UTC"

> ymd_hm("2017-11-28T14:02")
[1] "2017-11-28 14:02:00 UTC"

> ymd_h("2017-11-28T14")
[1] "2017-11-28 14:00:00 UTC"
> 

> ydm_hms("2017-22-12 10:00:00")
[1] "2017-12-22 10:00:00 UTC"

> ydm_hm("2017-22-12 10:00")
[1] "2017-12-22 10:00:00 UTC"

> ydm_h("2017-22-12 10")
[1] "2017-12-22 10:00:00 UTC"
> 
> 
> mdy_hms("11/28/2017 1:02:03") 
[1] "2017-11-28 01:02:03 UTC"

> mdy_hm("11/28/2017 1:02")
[1] "2017-11-28 01:02:00 UTC"

> mdy_h("11/28/2017 1")
[1] "2017-11-28 01:00:00 UTC"
> 
> 
> dmy_hms("1 Jan 2017 23:59:59") 
[1] "2017-01-01 23:59:59 UTC"

> dmy_hm("1 Jan 2017 23:59") 
[1] "2017-01-01 23:59:00 UTC"

> dmy_h("1 Jan 2017 23") 
[1] "2017-01-01 23:00:00 UTC"
> 
> ymd("20170131") 
[1] "2017-01-31"

> ydm("20173101")
[1] "2017-01-31"
> 
> 
> mdy("July 4th, 2000") 
[1] "2000-07-04"

> myd("July 2000,4th")
[1] "2000-07-04"
> 
> dmy("4th of July '99") 
[1] "1999-07-04"

> dym("4th 99 July")
[1] "1999-07-04"
> 
> yq("2001: Q3")
[1] "2001-07-01"
> 
> x=dmy_hms("12-JAN-19 11:45:20")
> date(x) # Date component. date(dt)
[1] "2019-01-12"

> year(x) #Year. year(dt)
[1] 2019

> isoyear(x) #The ISO 8601 year.
[1] 2019

> epiyear(x) #Epidemiological year.
[1] 2019

> month(x, label=T, abbr=F) #Month.
[1] January
12 Levels: January < February < March < April < May < ... < December

> month(x, label=T, abbr=T) #Month.
[1] Jan
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < ... < Dec

> month(x, label=F, abbr=F) #Month.
[1] 1

> month(x, label=F, abbr=T) #Month.
[1] 1

> month(x)
[1] 1

> day(x) #Day of month. day(dt)
[1] 12

> wday(x,label=T,abbr=T) #Day of week.
[1] Sat
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

> qday(x) #Day of quarter.
[1] 12

> hour(x) #Hour. hour(dt)
[1] 11

> minute(x) #Minutes. minute(dt)
[1] 45

> second(x) #Seconds. second(dt)
[1] 20

> week(x) #Week of the year. week(dt)
[1] 2

> isoweek(x) #ISO 8601 week.
[1] 2

> epiweek(x) #Epidemiological week.
[1] 2

> quarter(x) #Quarter. quarter(dt)
[1] 1

> 
> #semester(x, with_year = FALSE)
> #Semester. semester(dt)
> semester(x)
[1] 1
> 

> am(x) #Is it in the am? am(dt)
[1] TRUE

> pm(x) #Is it in the pm? pm(dt)
[1] FALSE

> dst(x) #Is it daylight savings? dst(d)
[1] FALSE

> leap_year(x) #Is it a leap year?
[1] FALSE