Skip to content

Instantly share code, notes, and snippets.

@mvfsillva
Created January 26, 2024 03:56
Show Gist options
  • Save mvfsillva/7b2f472e85a00cf0c0b59f5018653db6 to your computer and use it in GitHub Desktop.
Save mvfsillva/7b2f472e85a00cf0c0b59f5018653db6 to your computer and use it in GitHub Desktop.
tropical-flora-5517
String calculateAge(DateTime birthdate) {
DateTime currentDate = DateTime.now();
int years = currentDate.year - birthdate.year;
int months = currentDate.month - birthdate.month;
int days = currentDate.day - birthdate.day;
if (days < 0) {
DateTime lastMonth =
DateTime(currentDate.year, currentDate.month - 1, birthdate.day);
int daysInLastMonth = (currentDate.difference(lastMonth).inDays).floor();
months = months - 1;
days = daysInLastMonth - 1;
}
if (months < 0) {
years = years - 1;
months = months + 12;
}
return '$years years, $months months, $days days';
}
void main() {
DateTime birthdate1 = DateTime(2018, 3, 12);
DateTime birthdate2 = DateTime(2019, 7, 25);
DateTime birthdate3 = DateTime(2020, 11, 18);
DateTime birthdate5 = DateTime(2020, 1, 30);
DateTime birthdate8 = DateTime(2020, 8, 3);
DateTime birthdate11 = DateTime(2020, 10, 3);
DateTime birthdate13 = DateTime(2020, 8, 27);
DateTime birthdate19 = DateTime(2020, 7, 9);
DateTime birthdate6 = DateTime(2021, 5, 9);
DateTime birthdate12 = DateTime(2021, 2, 19);
DateTime birthdate14 = DateTime(2021, 1, 11);
DateTime birthdate17 = DateTime(2021, 9, 17);
DateTime birthdate7 = DateTime(2021, 12, 22);
DateTime birthdate10 = DateTime(2022, 4, 7);
DateTime birthdate18 = DateTime(2022, 3, 25);
DateTime birthdate15 = DateTime(2022, 5, 8);
DateTime birthdate4 = DateTime(2023, 9, 6);
DateTime birthdate9 = DateTime(2023, 6, 14);
DateTime birthdate16 = DateTime(2023, 12, 1);
DateTime birthdate20 = DateTime(2023, 11, 5);
DateTime pamCase = DateTime(2023, 1, 26);
DateTime marcusCase = DateTime(2023, 7, 18);
DateTime birthdate21 = DateTime(2024, 1, 24);
DateTime birthdate22 = DateTime(2024, 1, 1);
print(calculateAge(birthdate1));
print(calculateAge(birthdate2));
print(calculateAge(birthdate3));
print(calculateAge(birthdate5));
print(calculateAge(birthdate8));
print(calculateAge(birthdate11));
print(calculateAge(birthdate13));
print(calculateAge(birthdate19));
print(calculateAge(birthdate6));
print(calculateAge(birthdate7));
print(calculateAge(birthdate12));
print(calculateAge(birthdate14));
print(calculateAge(birthdate17));
print(calculateAge(birthdate10));
print(calculateAge(birthdate15));
print(calculateAge(birthdate18));
print(calculateAge(birthdate4));
print(calculateAge(birthdate9));
print(calculateAge(birthdate16));
print(calculateAge(birthdate20));
print(calculateAge(birthdate21));
print(calculateAge(birthdate22));
print(calculateAge(pamCase));
print(calculateAge(marcusCase));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment