从给定的出生日期计算年龄

编程入门 行业动态 更新时间:2024-10-27 16:30:02
本文介绍了从给定的出生日期计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 2 个组合框和 2 个文本框.我的第一个组合框以这种格式包含月份,一月、二月等,另一个组合框包含从 1 到 31 的数字.我的第一个文本框是 txtyear.一旦用户向 txtyear 输入出生年份,变量 BOD 将等于此.

Dim BOD 作为字符串BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text

我最后一个文本框的目的是处理当光标在txtyear上失去焦点时将计算的用户年龄.

谁能帮忙计算年龄.

解决方案

这里真的有两个问题:

  • 如何将输入的字符串转换为DateTime对象
  • 如果数据格式正确,如何计算年龄.
  • 我会让你按照其他人的说明使用 TryParseExtract 这绝对是正确的方法.

    根据某人的出生日期确定某人的年龄时,请尝试使用此方法:

    公共函数 GetCurrentAge(ByVal dob As Date) As IntegerDim 年龄为整数年龄 = Today.Year - dob.Year如果 (dob > Today.AddYears(-age)) 那么年龄 -= 1返回年龄结束函数

    这是 Jeff Atwood 非常受欢迎的问题 如何计算某人的年龄的顶级答案的 vb 版本

    我写了一篇关于从 dob 计算年龄 也是.

    I have 2 comboboxes and 2 textboxes. My first combobox contains months in this format january, february, etc, and the other combobox contains numbers from 1 to 31. My first textbox is txtyear. Once the user input birth year to txtyear a variable BOD will be equals to this.

    Dim BOD as string BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text

    The purpose of my last textbox is to handle the age of the user that will be computed when the cursor lost focus on txtyear.

    Can anyone help how to compute the age.

    解决方案

    There are really two questions here:

  • How to convert the string input into a DateTime object
  • How to calculate age once you have your data in the correct format.
  • I'll let you follow other's instructions for how use TryParseExtract which is definitely the correct way to go here.

    When determining someone's age from their DOB, try using this:

    Public Function GetCurrentAge(ByVal dob As Date) As Integer Dim age As Integer age = Today.Year - dob.Year If (dob > Today.AddYears(-age)) Then age -= 1 Return age End Function

    It is the vb version of the top answers on Jeff Atwood's very popular question How do I calculate someone's age

    I wrote a blogpost about calculating age from dob as well.

    更多推荐

    从给定的出生日期计算年龄

    本文发布于:2023-10-18 11:45:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1504141.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:出生日期   年龄

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!