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