When I saw such an algorithm on the internet, I felt very clever at the beginning,
if (DateTime.Now.Subtract(time).Days / 365 >60)
Later I thought there seemed to be a problem:
For example, assume that today is July 22, July 28, 2014 and someone was born on July 15, July 28, 1954. According to this algorithm, the result is: 21915/365 = 60.04109589041096, greater than 60. If this person was born on February 1, July 29, 1954, and in my opinion, is under 60 years old, and the result is 21914/365 = 60.03835616438356, still greater than 60, obviously incorrect
Well, follow the human mind:
1 Private Static bool isadult (datetime birthday) 2 {3 datetime now = datetime. now; 4 If (now. year-birthday. year <18) // If the year is less than 18, false is directly returned. Minor 5 {6 RETURN false; 7} 8 else if (now. year-birthday. year = 18) // If the year difference is equal to 18, compare the Month 9 {10 if (now. month> birthday. month) // If the year is 18, false is returned if the current month is less than the month of birth, and 11 {12 Return false; 13} 14 else if (now. month = birthday. month) // If the month is equal, compare the date 15 {16 if (now. day> birthday. day) // If the year is 18 or the month is equal, false is returned if the current date is less than the date of birth, and 17 {18 return false; 19} 20} 21} 22 return true; 23}
View code
The same is true when you are 60 years old.