与年龄计算有关的问题 [英] Question related to age calculation

查看:125
本文介绍了与年龄计算有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生我正在使用三个下拉列表和三个下拉列表中的一个文本框我显示日期月份

i想要使用所有下拉列表计算年龄并在文本框中显示总年龄

解决方案

  1. 根据 DropDownList DateTime 对象$ c>条目,比如 dtALongTimeAgo
  2. 获取 TimeSpan 对象,比如 tsElapsed ,因为 DateTime.Today dtALongTimeAgo
  3. 之间的差异
  4. 可以免费使用 tsElapsed 属性来检索所需的所有信息。


< blockquote>试试这个,根据你的需要自定义它。



日: <   asp:DropDownList     runat   =  server    ID   =  ddlD >  
< / asp:DropDownList >
月:< asp:DropDownList runat = server ID = ddlM >
< / asp:DropDownList >
年份:< asp:DropDownList runat = server ID = ddlY >
< / asp:DropDownList >


< asp:标签 runat = server ID = lblAge > < / asp:标签 >
< asp:按钮 ID = btnCalculate runat = server 文本 = 计算 OnClick = btnCalculate_Click / >







CS



  protected   void  Page_Load( object  sender, EventArgs e)
{


if (!IsPostBack)
{
for int i = 1 ;我< = 31 ; i ++)
ddlD.Items.Add(i.ToString());

for int i = 1 ; i < = 12 ; i ++)
ddlM.Items 。新增(i.ToString());

for int i = 1 ; i < = 75 ; i ++)
ddlY.Items .Add((DateTime.Now.Year - i).ToString());

}
}


受保护 void btnCalculate_Click( object sender,EventArgs e)
{
int day,month,year;
int .TryParse(ddlD.Text, out 天);
int .TryParse(ddlM.Text, out month);
int .TryParse(ddlY.Text, out year);

DateTime target = new DateTime(年,月,日);
DateTime now = DateTime.Now;
年龄= 年龄(目标,现在);
lblAge.Text = string .Format( {0}年,{1}个月,{2}天,age.Years,age.Months,age.Days);

}





使用 [ ^ ]代码用于计算年龄,只需将其添加到您的代码中


sir i am using three dropdownlist and and one text box in three dropdownlist i am showing date month year
i want to calculate age using all dropdownlist and display total age in the text box

解决方案

  1. Create a DateTime object based on the DropDownList entries, say dtALongTimeAgo
  2. Get the TimeSpan object, say tsElapsed, as the difference between DateTime.Today and dtALongTimeAgo
  3. Fell free to use the tsElapsed properties in order to retrieve all the info you need.


try this, customize it based on your need.

Day:<asp:DropDownList runat="server" ID="ddlD">
      </asp:DropDownList>
      Month:<asp:DropDownList runat="server" ID="ddlM">
      </asp:DropDownList>
      Year:<asp:DropDownList runat="server" ID="ddlY">
      </asp:DropDownList>


      <asp:Label runat="server" ID="lblAge"></asp:Label>
      <asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" />




CS

protected void Page_Load(object sender, EventArgs e)
        {
             

            if (!IsPostBack)
            { 
                for (int i = 1; i <= 31; i++)
                    ddlD.Items.Add(i.ToString());

                for (int i = 1; i <= 12; i++)
                    ddlM.Items.Add(i.ToString());

                for (int i = 1; i <= 75; i++)
                    ddlY.Items.Add((DateTime.Now.Year - i).ToString());
                 
            }
        }
       

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            int day,month,year ;
            int.TryParse(ddlD.Text, out day);
            int.TryParse(ddlM.Text, out month);
            int.TryParse(ddlY.Text, out year);

            DateTime target = new DateTime(year, month, day);
            DateTime now = DateTime.Now;
            Age age = new Age(target, now);
            lblAge.Text = string.Format(" {0} years, {1} months, and {2} days", age.Years, age.Months, age.Days);
            
        }



used this[^]code for calculating the age, just add it in your code


这篇关于与年龄计算有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆