年龄查询条件代码 [英] code for searching condition for age

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

问题描述

我们用一个文本框输入出生日期.将文本框中的出生日期与当前日期进行比较,然后计算出准确的年龄.如果计算出的年龄小于18,则显示错误消息.为此请提供代码.

we take a textbox to enter date of birth.The date of birth in the textbox is compared with current date then calculate exact age.If the calculated age is less than 18,display error message.kindly give the code for this.

推荐答案

根据您的要求更改示例代码

Sample code change as per your requirement

bool m_flag = false;
DateTime dt = new DateTime(1983, 5, 1, 8, 30, 52);
        DateTime dt1 = DateTime.Now;
        string strDate = dt.ToString("M/dd/yyyy");
        string strDate1 = dt1.ToString("M/dd/yyyy");
        if (compareDates(Convert.ToDateTime(strDate1), Convert.ToDateTime(strDate)))
        {
            m_flag = true;
            Response.Write("Valid");
        }
        else
        {
            Response.Write("Invalid");
        }

<pre>public bool compareDates(DateTime dtHireDate, DateTime dtDateOfBirth)
    {
        int age = dtHireDate.Year - dtDateOfBirth.Year;
        if (dtHireDate.Month < dtDateOfBirth.Month || (dtHireDate.Month == dtDateOfBirth.Month && dtHireDate.Day < dtDateOfBirth.Day))
            age--;
        if (age >= 18)
        {
            m_flag = true;
        }
        else
        {
            m_flag = false;
        }
        return m_flag;
    }



这篇关于年龄查询条件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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