需要根据选择值计算年龄 [英] Need to calculate the age from the select value
本文介绍了需要根据选择值计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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();
}
}
推荐答案
如果您希望逻辑希望对您有所帮助: ^ ]
If you want the logic hope this will help you:http://stackoverflow.com/questions/9/calculating-age-from-birthday[^]
System.DateTime date1 = new System.DateTime(2012, 1, 1);
System.DateTime date2 = System.DateTime.Now.Date;
// diff1 gets 242 days
System.TimeSpan diff1 = date2.Subtract(date1);
这篇关于需要根据选择值计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文