Bug 1592536 - Fix contact age calculation in address book. r=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 04 Nov 2019 21:59:45 +1300
changeset 37428 7cd9756e2aa1aa0322967ea1d6b309631ca73c5f
parent 37427 fee24a698db44e38cf473a849d27398e83717989
child 37429 39b07c84497e3ef43bf7e672d9fb97bd6a9644f1
push id396
push userclokep@gmail.com
push dateMon, 06 Jan 2020 23:11:57 +0000
reviewersjorgk
bugs1592536
Bug 1592536 - Fix contact age calculation in address book. r=jorgk
mail/components/addrbook/content/abCard.js
--- a/mail/components/addrbook/content/abCard.js
+++ b/mail/components/addrbook/content/abCard.js
@@ -844,36 +844,40 @@ function calculateAge() {
 
   if (birthMonth == -1 || birthDay == -1) {
     return;
   }
   if (isNaN(birthYear) || birthYear < kMinYear || birthYear > kMaxYear) {
     return;
   }
 
+  birthMonth--; // Date object months are 0-indexed.
   let today = new Date();
   let age = today.getFullYear() - birthYear;
   if (birthMonth > today.getMonth()) {
     age--;
   } else if (birthMonth == today.getMonth() && birthDay > today.getDate()) {
     age--;
   }
-  document.getElementById("Age").value = age;
+  if (age >= 0) {
+    document.getElementById("Age").value = age;
+  }
 }
 
 function calculateYear() {
   let age = document.getElementById("Age").value;
   if (isNaN(age)) {
     return;
   }
 
   let today = new Date();
   let year = today.getFullYear() - age;
 
   let birthMonth = document.getElementById("BirthMonth").value;
+  birthMonth--; // Date object months are 0-indexed.
   let birthDay = document.getElementById("BirthDay").value;
   if (birthMonth > today.getMonth()) {
     year--;
   } else if (birthMonth == today.getMonth() && birthDay > today.getDate()) {
     year--;
   }
   document.getElementById("BirthYear").value = year;
   setDisabledMonthDays();