C# 每次计算人的年龄都显示相同的数字
我想通过输入某人的生日来计算他的年龄,并在文本框中显示出来,但它一直给我相同的准确年龄,即2013年,我不知道为什么需要任何帮助,下面是代码C# 每次计算人的年龄都显示相同的数字,c#,C#,我想通过输入某人的生日来计算他的年龄,并在文本框中显示出来,但它一直给我相同的准确年龄,即2013年,我不知道为什么需要任何帮助,下面是代码 string constring = "datasource=localhost;port=3306;username=root;password=root"; int orderID = 0; orderID = Int32.Parse(textBox74.Text); int bd
string constring = "datasource=localhost;port=3306;username=root;password=root";
int orderID = 0;
orderID = Int32.Parse(textBox74.Text);
int bday = orderID;
DateTime B_DAY = new DateTime(bday);
DateTime Today = DateTime.Today;
int age2 = Today.Year - B_DAY.Year;
if(B_DAY > Today.AddYears(-age2))
age2-- ;
string theage = age2.ToString();
this.textBox70.Text = theage;
即使您的代码不清楚,并且在这里造成混乱,您如何以一种巧妙的方式计算年龄
static void Main(string[] args)
{
DateTime birthDate = new DateTime(1969,12,25);
int age = (int) ((DateTime.Now - birthDate).TotalDays/365);
}
你可以简单地使用它,而不是处理太多的混乱
DateTime drid2 = Convert.ToDateTime(textBox74.Text);
DateTime drid3 = DateTime.Now;
int yy1 = Math.Abs(drid1.Year - drid2.Year);
textBox70.Text = yy1.ToString();
Int32.ParsetextBox74.Text;您是否正在尝试将字符串格式的日期解析为int?这是一段令人惊讶的混乱代码,因为它所包含的内容非常少。在调试器中单步执行此操作时,运行时值是什么?你的计算在哪里产生了与你预期不同的结果?通过阅读代码甚至很难确定你想做什么。猜测一下,他在文本框中输入了一个格式类似于1989年11月16日的日期,或者可能是11月16日,通过int将其转换为DateTime没有任何区别。当然,因为C DateTimes是从0001年1月1日00:00:00.000起经过的100纳秒间隔数,这基本上是零日期,这导致了报告的行为。@NahuelI。我认为这个位是Int32.ParsetextBox74.Text;是解析ID而不是日期。