如何使用C#计算年龄 [英] How to calculate Age using C#
本文介绍了如何使用C#计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨先生
我想说我正在创建一个正常工作的注册表格。
i为了获得年龄而做了代码但是它不能正常工作。
i需要年,月,但我的代码只显示年份将如何获得年月和&天
这是我的代码
hi sir
I Want to say that i am creating a registration form to work properly.
i did code for get age but it doesn't work properly .
i want year, month day but my code is only show year how will get year month & days
this is my code
private void button1_Click(object sender, EventArgs e)
{
DateTime dob = Dob_dateTimePicker.Value;
DateTime PresentYear = Current_dateTime.Value;
TimeSpan ts = PresentYear - dob;
int Age = ts.Days / 365;
textBox1.Text = Age.ToString() ;+//" Year" + "Month" +"Days";
}
请解释我如何获得年龄=年,月&天
问候
Umashankar sahu
Please Explain Me How To Get Age = Year ,Month & Days
Regards
Umashankar sahu
推荐答案
DateTime dob = Convert.ToDateTime("18 Feb 1987");
DateTime PresentYear = DateTime.Now;
TimeSpan ts = PresentYear - dob;
DateTime Age = DateTime.MinValue.AddDays(ts.Days);
MessageBox.Show(string.Format(" {0} Years {1} Month {2} Days", Age.Year - 1, Age.Month - 1, Age.Day - 1));
Go通过这个。你有完整的年龄计算代码。
http://raasukutty.wordpress.com/2009/06/18/c-calculate-age-in-years-month-and-days/ [ ^ ]
Go through this. you have the complete code for age calculation.
http://raasukutty.wordpress.com/2009/06/18/c-calculate-age-in-years-month-and-days/[^]
Hi,
DateTime age = new DateTime(PresentYear.Subtract(dob).Ticks);
应该做好这份工作。如果要输出可读格式,
Should do the job. If you want to output a readable format,
textBox1.Text = age.Year.ToString() + " years " + age.Month.ToString() + " months " + age.Day.ToString() + " days ";
应该没问题。
问候。
should be OK.
Regards.
这篇关于如何使用C#计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文