How to calculate Years between Dates in R ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report To calculate the number of years between two dates we have several methods. In this article, we will discuss all the methods and their examples of how to calculate the number of years or the difference of years between two dates in the R Programming Language. Table of ContentMethod 1: Using the seq() functionMethod 2: Using the difftime functionMethod 3: Using the base R as.POSIXlt functionExample:Input: Date_1 = 2020/02/21 Date_2 = 2023/03/21 Output: 3 Explanation: In Date_1 and Date_2 have three years difference in year. Here we will use the seq() function to get the result. This function is used to create a sequence of elements in a Vector. Syntax: length(seq(from=date_1, to=date_2, by=’year’)) -1 Method 1: Using the seq() functionExample 1: R # creating date_1 variable and # storing date in it. date_1<-as.Date("2020-08-10") # creating date_2 variable and #storing date in it. date_2<-as.Date("2023-10-10") # Here first date will start from # 2020-08-10 and end by 2023-10-10. # Here increment is done by year. # This three dates will be generated # as we used eq and this dates will # be stored in a. a = seq(from = date_1, to = date_2, by = 'year') print(a) # Here we are finding length of a and # we are subtracting 1 because we dont # need to include current year. print(length(a)-1) Output: [1] "2020-08-10" "2021-08-10" "2022-08-10" "2023-08-10"[1] 3Example 2: R # creating date_1 variable and # storing date in it. date_1<-as.Date("2020-08-10") # creating date_2 variable and # storing date in it. date_2<-as.Date("2100-08-10") # Here first date will start from # 2020-08-10 and end by 2100-08-10. # Here increment is done by year. # This three dates will be generated # as we used seq and this dates will # be stored in a. a = seq(from = date_1, to = date_2, by = 'year') print(a) # Here we are finding length of a and # we are subtracting 1 because we dont # need to include current year. print(length(a)-1) Output: [1] "2020-08-10" "2021-08-10" "2022-08-10" "2023-08-10" "2024-08-10" [6] "2025-08-10" "2026-08-10" "2027-08-10" "2028-08-10" "2029-08-10"[11] "2030-08-10" "2031-08-10" "2032-08-10" "2033-08-10" "2034-08-10"[16] "2035-08-10" "2036-08-10" "2037-08-10" "2038-08-10" "2039-08-10"[21] "2040-08-10" "2041-08-10" "2042-08-10" "2043-08-10" "2044-08-10"[26] "2045-08-10" "2046-08-10" "2047-08-10" "2048-08-10" "2049-08-10"[31] "2050-08-10" "2051-08-10" "2052-08-10" "2053-08-10" "2054-08-10"[36] "2055-08-10" "2056-08-10" "2057-08-10" "2058-08-10" "2059-08-10"[41] "2060-08-10" "2061-08-10" "2062-08-10" "2063-08-10" "2064-08-10"[46] "2065-08-10" "2066-08-10" "2067-08-10" "2068-08-10" "2069-08-10"[51] "2070-08-10" "2071-08-10" "2072-08-10" "2073-08-10" "2074-08-10"[56] "2075-08-10" "2076-08-10" "2077-08-10" "2078-08-10" "2079-08-10"[61] "2080-08-10" "2081-08-10" "2082-08-10" "2083-08-10" "2084-08-10"[66] "2085-08-10" "2086-08-10" "2087-08-10" "2088-08-10" "2089-08-10"[71] "2090-08-10" "2091-08-10" "2092-08-10" "2093-08-10" "2094-08-10"[76] "2095-08-10" "2096-08-10" "2097-08-10" "2098-08-10" "2099-08-10"[81] "2100-08-10"[1] 80Method 2: Using the difftime function R # Example dates date1 <- as.Date("2021-01-01") date2 <- as.Date("2023-08-01") # Calculate the difference in weeks weeks_diff <- difftime(date2, date1, units = "weeks") print(weeks_diff) # Calculate and print the year difference year_diff <- weeks_diff / 52 print(paste('Year difference:', year_diff)) # Calculate the difference in days days_diff <- difftime(date2, date1, units = "days") print(days_diff) # Calculate and print the year difference year_diff <- days_diff / 365 print(paste('Year difference:', year_diff)) Output: Time difference of 134.5714 weeks[1] "Year difference: 2.58791208791209"Time difference of 942 days[1] "Year difference: 2.58082191780822"Method 3: Using the base R as.POSIXlt function R # Example dates date1 <- as.POSIXlt("2021-01-01") date2 <- as.POSIXlt("2023-08-01") # Calculate the difference in years years_diff <- date2$year - date1$year years_diff Output: [1] 2methods Comment More infoAdvertise with us Next Article Calculate Time Difference between Dates in R Programming - difftime() Function B bhagiradhrayini25 Follow Improve Article Tags : R Language R-DateTime Similar Reads How to calculate number of days between two dates in R ? In this article, we will discuss how to Find the number of days between two dates in the R programming language. Working with Dates in RDates play a crucial role in data analysis, and figuring out the time difference between two dates is a typical job. Calculating the number of days between two dat 2 min read How to Extract Year from Date in R In this article, we are going to see how to extract the year from the date in R Programming Language. Method 1:  Extract Year from a Vector In this method, the as.POSIXct is a Date-time Conversion Functions that is used to manipulate objects of classes. To extract the year from vector we need to cre 2 min read How to Use Date Formats in R In this article, we will be looking at the approaches to using the date formats in the R programming language, R programming language provides several functions that deal with date and time. These functions are used to format and convert the date from one form to another form. R provides a format fu 2 min read Calculate Time Difference between Dates in R Programming - difftime() Function difftime() function in R Language is used to calculate time difference between dates in the required units like seconds, minutes, days, weeks, etc. Syntax: difftime(date1, date2, units) Parameters:date1, date2: Dates to calculate difference units: seconds, minutes, days, weeksExample 1:R# R program 1 min read Calculate Time Difference between Dates in R Programming - difftime() Function difftime() function in R Language is used to calculate time difference between dates in the required units like seconds, minutes, days, weeks, etc. Syntax: difftime(date1, date2, units) Parameters:date1, date2: Dates to calculate difference units: seconds, minutes, days, weeksExample 1:R# R program 1 min read How to Calculate Age in Excel: Step by Step Tutorial Calculating age in Excel is a simple yet important task, whether you're tracking employee birthdays, analyzing customer data, or working on a personal project. While the DATEDIF function is commonly used for this, plenty of other methods are available to calculate age accurately. Excel offers severa 7 min read Like