本文介绍了得到一个人的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用三个下拉列表来选择下拉列表返回值的日期(1998年9月12日)
我正在尝试计算年龄,但出现错误
HI I am using three drop downs to select the date the drop downs look return the values 1998 september 12
i am trying to calculate the age but get an error
ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
{
MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
MonthNumber = a
//DataTextField = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
//DataValueField = a
}
);
//If this code is not working then try the above code in the loop.
ddlMonth.DataTextField = "MonthName";
ddlMonth.DataValueField = "MonthNumber";
ddlMonth.DataBind();
ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
ddlYear.DataBind();
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
ddlday.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedIndex + 1))); ddlday.DataBind();
}
private int _GetAge()
{
//int Age = Convert.ToInt32((DateTime.Now.Subtract().TotalDays / 365.25).ToString("#"));
//ddlYear.SelectedValue.ToString();
//ddlMonth.SelectedValue.ToString();
//ddlday.SelectedValue.ToString();
string age = (ddlYear.SelectedItem.ToString() + ddlMonth.SelectedItem.ToString() + ddlday.SelectedItem.ToString());
age.Substring(2, age.Length - 2);
//age = age.PadRight(13, 0);
IDValidator dateSelect = new IDValidator(age.ToString());
if (!dateSelect.get_IsValidDate(true))
throw new ArgumentException("Date not Valid");
return dateSelect.get_Age(DateTime.Now);
}
推荐答案
DateTime today = DateTime.Today; // 08/30/2012
DateTime birthday = DateTime.MinValue;
DateTime.TryParse(age, out birthday);// 08/30/2000
string actual_age = string.Empty;
if (birthday.DayOfYear <= today .DayOfYear)
{
actual_age = (today .Year-birthday.Year).ToString();
}
else
{
actual_age = (today .Year-birthday.Year- 1).ToString();
}
希望对您有所帮助.
Hope it helps.
这篇关于得到一个人的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!