getYearsAgo()function

getYearsAgo(target: PossibleDate, current?: PossibleDate, caller: AnyCaller = getYearsAgo): number
ParamType
targetPossibleDate
Date to count back from. required
currentPossibleDate
Date to count to (defaults to now).
callerAnyCaller
Function to attribute a thrown error to (defaults to getYearsAgo itself). Defaults to getYearsAgo
Return
number
Number of calendar years from target to current (negative if target is in the future).
Throws
RequiredError
If target or current cannot be converted to a valid date.

Count the calendar years since a date.

  • Note this counts calendar years, not 365-day periods.
  • e.g. from December 31st to January 1st is -1 years, even though it's only 1 day.

Examples

getYearsAgo(target) // 2