你想在没有JavaScript的情况下用C#计算年龄? [英] HOw to Calculate Age in C# without JavaScript?

查看:91
本文介绍了你想在没有JavaScript的情况下用C#计算年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有2个文本框。第一个是出生日期,第二个是年龄。



当我在1st Textbox1中输入出生日期然后在第二个文本框中自动计算年龄。



任何人都可以帮助我/

如何在没有JavaScript的情况下计算年龄?



 <%@     Page    语言  =  C#    AutoEventWireup   =  true    CodeFile   =  Age.aspx.cs   继承  = 百分比   %>  

< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< / head >
< body >
< 表格 id = form1 runat = server >
< div >
< >
< tr >
< td >
DO B
< / td >
< td >
< asp:TextBox ID = txtDOB runat = 服务器 > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td >
年龄< / td >
< < span class =code-leadattribute> td >
< asp:TextBox ID = txtAge runat = server > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td >
< asp:按钮 ID = Button1 runat = server 文字 = 计算 / >
< / td >
< / tr >
< / table >
< / div >
< < span class =code-leadattribute> / form >
< / body >
< / html >

解决方案

在这里试试这个:

在你的Button OnClick 活动中



 DateTime bday = Convert.ToDateTime(txtDOB.Text); 
今日DateTime = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age))age--;

txtAge.Text = age.ToString();





希望有帮助



Azee ......


这很痛苦,不是吗?



见这里:使用Age:它与TimeSpan不同! [ ^ ]


嗯,这个答案可能很多!是的,dob的年龄计算可以有多种方式。绝对取决于您从用户输入的格式。最简单的可能是:

  var  today =  float  .Parse(DateTime.Now.ToString(  yyyy.MMdd)); 
var inputDob = float .Parse(dateOfBirth.ToString( yyyy.MMdd));
var age =( int )(今天 - inputDob);



哪里 dateOfBirth 是你的输入。

我建议你看看这个链接哪里有一些很好的选择。看哪一个适合你:

如何计算某人的年龄在C# [ ^ ]


Hello,

I have 2 Textbox. First is Date of Birth and Second is Age.

When I enter Date of Birth in 1st Textbox1 then Automatically Calculate Age in 2nd Textbox.

Can Anyone Please Help me/
How to Calculate Age Without JavaScript?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Age.aspx.cs" Inherits="Percentage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    DOB
                </td>
                <td>
                    <asp:TextBox ID="txtDOB" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Age</td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server" "></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Calculate" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

解决方案

Here try this:
In your Button's OnClick event

DateTime bday = Convert.ToDateTime(txtDOB.Text);
           DateTime today = DateTime.Today;
           int age = today.Year - bday.Year;
           if (bday > today.AddYears(-age)) age--;

           txtAge.Text = age.ToString();



Hope it helps

Azee...


It's a pain, isn't it?

See here: Working with Age: it's not the same as a TimeSpan![^]


Well,this answer could be numerous! Yes, age calculation from dob could be a number of ways. Absolutely that depends on which format you are taking input from user. The simplest one could be like:

var today = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
var inputDob = float.Parse(dateOfBirth.ToString("yyyy.MMdd"));
var age = (int)(today - inputDob);


Where dateOfBirth is your input.
And i would recommend you to see this link where there are some excellent alternatives.See which one suits you:
How do I calculate someone's age in C#[^]


这篇关于你想在没有JavaScript的情况下用C#计算年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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