In Order to get difference between two dates in R by days, weeks, months and years. We will be using difftime() function. Let’s see how to

- Get difference between two dates in R by days with an example.
- Get difference between two dates in R by weeks with an example.
- Get difference between two dates in R by months with an example.
- Get difference between two dates in R by years with an example.

Let’s first create the dataframe.

### Create Data Frame df1 = data.frame ( Name =c('Annie','Catherine','Teresa','Peterson','Richard','joe'), Date_of_birth = as.Date(c('1995-06-16','1991-04-19','1993-07-22','1990-03-26','1991-05-12','1992-09-13')), Date_of_joining = as.Date(c('2018-06-03','2018-06-03','2018-06-03','2018-06-036','2018-06-03','2018-06-03'))) df1

dataframe df1 will be

**Get difference between two dates in R by days with an example:**

Difference between two dates in R can be calculated using difftime function with argument units = “days” as shown below

# Difference in days df1$diff_in_days = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "days")) df1

So the resultant data frame will be

**Get difference between two dates in R by weeks with an example:**

Difference between two dates in R by weeks can be calculated using difftime function with argument units = “weeks” as shown below

# Difference in weeks df1$diff_in_weeks = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "weeks")) df1

So the resultant data frame will be

**Get difference between two dates in R by months with an example:**

Difference between two dates in R by months can be calculated using difftime function in roundabout way with argument units = “days” divided by (365.25/12) as shown below

# difference in months df1$diff_in_months = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units ="days"))/(365.25/12) df1

So the resultant data frame will be

**Get difference between two dates in R by year with an example:**

Difference between two dates in R by year can be calculated using difftime function in roundabout way with argument units = “weeks” divided by 52.25 as shown below

# difference in years df1$diff_in_years = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "weeks"))/52.25 df1

So the resultant data frame will be

**Get difference between two dates in R by quarter with an example:**

Difference between two dates in R by quarter can be calculated using difftime function in roundabout way with argument units = “days” divided by (365.25/4) as shown below

# difference in quarter df1$diff_in_quarter = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "days"))/(365.25/4) df1

So the resultant data frame will be