countYears method
Calculates the number of years between this DateTime instance and another DateTime instance.
Returns an integer representing the number of years difference.
Example usage:
DateTime startDate = DateTime(1996, 1, 1);
DateTime endDate = DateTime(2023, 10, 16);
int yearsDifference = startDate.countYears(endDate);
print('Years Difference: $yearsDifference'); // Output: 27
Implementation
int countYears(DateTime? differenceDateTime) {
int difference =
(differenceDateTime ?? DateTime.now()).millisecondsSinceEpoch -
millisecondsSinceEpoch;
int count = (difference / 31536000000).truncate();
// return count.toString() + (count > 1 ? ' years' : ' year');
return count;
}