我必须通过填写dateofbirth来自动获得年龄 [英] I have to get age automatically by filling the dateofbirth

查看:75
本文介绍了我必须通过填写dateofbirth来自动获得年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经尝试了以下代码并且不知道我必须在哪里传递值,因为该值由用户填充。



我尝试了什么:



  private   static   int  CalculateAge(DateTime DOB)
{
int age = 0 ;
age = DateTime.Now.Year - DOB.Year;

if (DateTime.Now.DayOfYear < DOB.DayOfYear)
age = age - 1 ;

返回年龄;
}

受保护 void Calendar1_SelectionChanged1( object sender,EventArgs e)
{
txtbox2.Text = Calendar1.SelectedDate.ToString();
// int CalculateAge()

Calendar1.Visible = false ;
txtAge.Text = CalculateAge()
}

解决方案

我不太确定,如果你的

 Calendar1.SelectedDate 

已经是 DateTime



如果所以,你应该将它作为参数传递给你的函数。如果没有,你必须将值解析为 DateTime ,然后传递它。


我的理解你不确定是什么在事件发生之后,您应该在输入年龄后立即反映年龄,而不是在失去焦点之后。如果这是一个桌面应用程序,应该有一些on change事件或keypress事件。但是你必须在计算年龄之前检查日期是否有效。



希望这对你有帮助。


使用 DateTime.Parse DateTime.ParseExact Calendar1.SelectedDate.ToString()转换为DateTime对象,然后传递给它反对CalculateAge(..)方法。这就是全部。



示例:

在Calendar1_SelectionChanged1(...)中使用它,

 DateTime txtMyDate = 
DateTime.ParseExact(txtbox2.Text, M / d / yyyy,CultureInfo.InvariantCulture);

也可以使用 DateTime.TryParseExact


Hi I have tried the below code and dont know where I have to pass this with value because the value is filled by the user.

What I have tried:

private static int CalculateAge(DateTime DOB)
{
    int age = 0;
    age = DateTime.Now.Year - DOB.Year;

    if (DateTime.Now.DayOfYear < DOB.DayOfYear)
      age = age - 1;

    return age;
}

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
    txtbox2.Text = Calendar1.SelectedDate.ToString();
    //int CalculateAge()

    Calendar1.Visible = false;
    txtAge.Text= CalculateAge()
}

解决方案

I'm not quite sure, if your

Calendar1.SelectedDate

is already a DateTime.

If so, you should pass this as argument into your function. If not, you have to parse the value to DateTime and then pass it.


what i understand that you are not sure what is the event, you should handle to reflect the age immediately after typing the age not after lost focus. if this is a desktop application, there should have been some on change event or keypress event. But you must check the date is valid before calculating the Age.

Hope this would help you.


Use DateTime.Parse or DateTime.ParseExact to convert your Calendar1.SelectedDate.ToString() to a DateTime object and just pass this object to the CalculateAge(..) method. That's all.

Sample:
Use it inside Calendar1_SelectionChanged1(...),

DateTime txtMyDate = 
  DateTime.ParseExact(txtbox2.Text, "M/d/yyyy", CultureInfo.InvariantCulture);

Also can use DateTime.TryParseExact.


这篇关于我必须通过填写dateofbirth来自动获得年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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