C # based on the ID number, calculate birthday, age, gender

Source: Internet
Author: User

Friends talk about identity card-related information, only to understand the original province card number contains the age and gender.

This eliminates the need for separate fields to store them in the database (however, depending on the situation, if the reading frequency is high, or if it is listed separately), this solves the problem of age change in passing.

Program just to achieve this function, the inside still need data validation, the user input information, after all, in the Ape class, it is "illegal." Needless to say, put on the program I wrote, also please pass the Great God Treatise:

Using system;using system.collections.generic;using system.linq;using system.text;using System.text.regularexpressions;using system.threading.tasks;namespace calculateagebirthdatsexdemo{public class program {public static void Main (string[] args) {string identitycard = "32128119930718125X";            Casually spell, if there is a similar, purely funny ha birthdayagesex entity = new Birthdayagesex ();            Entity=getbirthdayagesex (Identitycard); if (Entity! = null) {Console.WriteLine (entity. Birthday + "-----" + entity. Sex + "-----" + entity.            Age);        } console.readline (); public static Birthdayagesex Getbirthdayagesex (string identitycard) {if (string.            IsNullOrEmpty (Identitycard)) {return null; } else {if (identitycard.length! = && Identitycard.length! = 18)//ID number only                15-bit or 18-bit other illegal {    return null;            }} birthdayagesex entity = new Birthdayagesex (); String strsex = String.            Empty; if (identitycard.length = = 18)//processing 18-digit ID number from the number to get birthday and gender code {entity.                Birthday = identitycard.substring (6, 4) + "-" + identitycard.substring (2) + "-" + identitycard.substring (12, 2);            Strsex = identitycard.substring (14, 3); } if (identitycard.length = =) {entity.                 Birthday = "+" + identitycard.substring (6, 2) + "-" + identitycard.substring (8, 2) + "-" + identitycard.substring (10, 2);            Strsex = identitycard.substring (12, 3); } entity. Age = Calculateage (entity. Birthday);//Calculate Age by Birthday if (int). Parse (strsex)% 2 = = 0)//gender code for even number is female odd number for male {entity.            Sex = "female"; } else {entity.            Sex = "male";   } return entity;     }///<summary>//based on date of birth, calculate the exact age///</summary>//<param name= "Birthda        TE "> Birthdays </param>//<returns></returns> public static int calculateage (string birthDay)            {DateTime birthdate=datetime.parse (BirthDay);            DateTime Nowdatetime=datetime.now;            int age = Nowdatetime.year-birthdate.year; Consider the factors of the month and day if (Nowdatetime.month < Birthdate.month | |            (Nowdatetime.month = = Birthdate.month && nowdatetime.day < Birthdate.day))             {age--;        } return age;        }///<summary>//define Birthday Age gender entity///</summary> public class Birthdayagesex            {public string Birthday {get; set;}            public int Age {get; set;}        public string Sex {get; set;} }    }}

C # Calculates birthdays, ages, and genders based on the ID number

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.