
Extract the year from different date formats
extract_year.Rd
A function that extracts the year from dates where the date can be in such formats as: DD/MM/YYYY
, YYYY-MM-DD
, MM/YYYY
, YYYY-MM
, YYYY
, DD Month YYYY
.
Details
The method works by extracting any patterns of 4 numbers in a row. Thus, the year must be in the format YYYY. I.e. cannot have formats such as DD/MM/YY.
Examples
extract_year(c('03/05/2021', '3rd of May 2022', '2023-05-03'))
#> [1] 2021 2022 2023