Determine whether a person is 18 years old and 60 years old Based on the date in the ID card number.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.