This function attempts to return the year from characters representing dates formats. The formats can be ambigous and varied within the same set. For example, it returns "1982" (or 9982 if century is ambiguous) from "11/20/82", "1982-11-20", "Nov. 20, 1982", "20 Nov 1982", "20-Nov-1982", "20/Nov/1982", "20 Nov. 82", "20 Nov 82". The function handles ambiguous centuries (e.g., 1813, 1913, 2013) by including a dummy place holder for the century place (i.e., 9913). Note that it may return warnings like "NAs introduced by coercion".
Examples
yearFromDate(1969, yearLast=TRUE)
#> [1] 1969
yearFromDate('10-Jul-71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10-Jul-1971', yearLast=TRUE) # --> 1971
#> [1] 1971
yearFromDate('10-19-71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10-19-1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('10-1-71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3-22-71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3-2-71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10-1-1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3-22-1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3-2-1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('10/Jul/71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10/Jul/1971', yearLast=TRUE) # --> 1971
#> [1] 1971
yearFromDate('10/19/71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10/19/1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('10/1/71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3/22/71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3/2/71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10/1/1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3/22/1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3/2/1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('10 mmm 71', yearLast=TRUE) # "mmm" is month abbreviation--> 9971
#> [1] NA
yearFromDate('5 mmm 71', yearLast=TRUE) # "mmm" is month abbreviation--> 9971
#> [1] NA
yearFromDate('10 19 71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10 19 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('10 1 71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3 22 71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('3 2 71', yearLast=TRUE) # --> 9971
#> [1] 9971
yearFromDate('10 1 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3 22 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('3 2 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('Oct. 19, 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('19 October 1969', yearLast=TRUE) # --> 1969
#> [1] 1969
yearFromDate('How you do dat?', yearLast=TRUE) # --> NA
#> [1] NA
yearFromDate('2014-07-03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014-7-03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014-07-3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014-7-3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014/07/03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014/7/03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014/07/3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014/7/3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014 07 03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014 7 03', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014 07 3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate('2014 7 3', yearLast=TRUE) # --> 2014
#> [1] 2014
yearFromDate(1969, yearLast=FALSE)
#> [1] 1969
yearFromDate('10-Jul-71', yearLast=FALSE) # --> 9971
#> [1] 9910
yearFromDate('10-Jul-1971', yearLast=FALSE) # --> 1971
#> [1] 1971
yearFromDate('10-19-71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('10-19-1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('10-1-71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('3-22-71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('3-2-71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('10-1-1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3-22-1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3-2-1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('10/19/71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('10/19/1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('10/1/71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('3/22/71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('3/2/71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('10/1/1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3/22/1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3/2/1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('10 mmm 71', yearLast=FALSE) # "mmm" is month abbreviation--> 9971
#> [1] NA
yearFromDate('5 mmm 71', yearLast=FALSE) # "mmm" is month abbreviation--> 9971
#> [1] NA
yearFromDate('10 19 71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('10 19 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('10 1 71', yearLast=FALSE) # --> 9910
#> [1] 9910
yearFromDate('3 22 71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('3 2 71', yearLast=FALSE) # --> 9971
#> [1] 9971
yearFromDate('10 1 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3 22 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('3 2 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('Oct. 19, 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('19 October 1969', yearLast=FALSE) # --> 1969
#> [1] 1969
yearFromDate('How you do dat?', yearLast=FALSE) # --> NA
#> [1] NA
yearFromDate('2014-07-03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014-7-03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014-07-3', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014-7-3', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014/07/03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014/7/03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014/07/3', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014/7/3', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014 07 03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014 7 03', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014 07 3', yearLast=FALSE) # --> 2014
#> [1] 2014
yearFromDate('2014 7 3', yearLast=FALSE) # --> 2014
#> [1] 2014