根据出生日期计算年龄

4

我有两个下拉框和两个文本框。第一个下拉框包含以"january, february"等格式表示的月份,另一个下拉框包含1到31的数字。我的第一个文本框是txtyear。一旦用户输入出生年份到txtyear中,变量BOD将等于这个年份。

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

我最后一个文本框的目的是处理用户的年龄,当光标从txtyear移开时将计算年龄。

有人能帮忙计算年龄吗?


2
这是 SO 上最早的问题之一,第9个问题。请先努力搜索问题。 - Hans Passant
1
抱歉,先生。我是编程新手。我已经搜索了那个,但我不理解大多数代码。 - joshua
那么,你提问的方式可能不太聪明。你很可能只会得到更多你不理解的答案。这当然是毫无意义的。一定要提到已经找到了这些答案,并解释你不理解的地方。发布你尝试过的代码,并说明为什么它没有达到你希望的效果。 - Hans Passant
1
谢谢您告诉我这件事。我会记住并确保在提问之前先做到这一点。 - joshua
Hans,实际上,由于第9个问题是针对C#的,而这个问题是针对VB的,所以这并不是完全相同的问题。尽管如此,这个问题仍然可以有更好的提问方式。 - ShoeMaker
6个回答

11

这里实际上有两个问题:

  1. 如何将字符串输入转换为 DateTime 对象
  2. 一旦数据格式正确,如何计算年龄。

我会让你按照其他人的指示使用 TryParseExtract,这绝对是正确的方法。

从出生日期确定某人的年龄时,可以尝试使用以下方法:

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

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

我也写了一篇关于从出生日期计算年龄的博客文章。


3

以下是使用Date类的年份和月份属性的另一种不同方式:

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

Dim dt As Date
If Date.TryParseExact(BOD, "MMMM-dd-yyyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
    Dim Age As New Date(Now.Subtract(dt).Ticks)
    MsgBox(String.Format("Your age is : {0} Years and {1} Months", Age.Year - 1, Age.Month - 1))

Else
    MsgBox("Birth Date is in wrong format")
End If

1

以下是在使用 Visual Studio 2012 VB.NET 语言时的技巧:

Private Sub dtpBOfD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBOfD.ValueChanged

        lblAge.Text = Age(dtpBOfD.Value)

    End Sub

    Public Shared Function Age(DOfB As Object) As String
        If (Month(Date.Today) * 100) + Date.Today.Day >= (Month(DOfB) * 100) + DOfB.Day Then
            Return DateDiff(DateInterval.Year, DOfB, Date.Today)
        Else
            Return DateDiff(DateInterval.Year, DOfB, Date.Today) - 1
        End If
    End Function

0

使用这个函数

Function String2Date(ByVal sDay As String, ByVal sMonth as String, ByVal sYear as String) As Date
   StdDateString = sMonth & " " & sDay & ", " & sYear
End Function

然后应用它..

Dim dt1 as Date = String2Date(ComboBox2.Text,ComboBox1.Text,txtYear.Text).ToShortDateString
Dim dt2 as Date = Now.ToShortDateString
Dim dt3 as TimeSpan = (dt2 - dt1)
Dim diff as Double = dt3.Days
Dim sAge as String

sAge = Str(Int(diff / 365)) & " Year "
diff = diff Mod 365
sAge = sAge & Str(Int(diff / 30)) & " Month(s)"
diff = diff Mod 30
sAge = sAge & Str(diff) & " Day(s)"

txtAge.Text = sAge

假设一年有365天(每四年有366天 - 如果使用365.25则大部分情况下可行),并且假设每个月都恰好有30天(2月为28(或29)天,多个月份为31天 - 如果使用30.45则大部分情况下足够接近)。以下答案在这些方面要好得多。 - ShoeMaker

0

若要获取完整的年龄信息,请使用以下C#代码。

public string calculateDays(int day, int Month, int year)
    {           
       int Diffyear;
       int DiffMonth;
       int DiffDay;
       int cuYear=DateTime.Now.Year;
       int cuMonth=DateTime.Now.Month;
       int cuDay=DateTime.Now.Day;
       string Age;
        Diffyear= cuYear-year;
        DiffMonth=cuMonth-Month;
        DiffDay=cuDay-day;
        if ((DiffMonth) < 0)
    {
        Diffyear -= 1;
    }
        if ((DiffDay) < 0)
        {
            DiffMonth -= 1;
            if ((cuMonth - 1) < 8)
            {
                if (((cuMonth - 1) % 2) == 0)
                {
                    if ((cuMonth - 1) == 2)
                        if (cuYear % 4 == 0)
                        {
                            DiffDay = 29 + DiffDay;
                        }
                        else
                        {
                            DiffDay = 28 + DiffDay;
                        }
                    else
                        DiffDay = 30 + DiffDay;
                }


                else

                    DiffDay = 31 + DiffDay;
            }
            else
            {
                if (((cuMonth - 1) % 2) == 0)
                {
                    DiffDay = 31 + DiffDay;
                }
                else
                {
                    DiffDay = 30 + DiffDay;
                }
            }
        }
        if ((DiffMonth) < 0)
        {
            DiffMonth = DiffMonth+12;
        }
        if (Diffyear < 0)
        {
            Diffyear = Diffyear * (-1);
        }
        if ((DiffDay) < 0)
        {
            DiffDay = DiffDay * (-1);
        }

       Age = "Age: " + Diffyear.ToString() + " year, " + DiffMonth.ToString() + " months, " + DiffDay.ToString() + " days";
        return Age;
    }

`


尽管这段代码片段可能解决了问题,但是包括解释真的有助于提高您的帖子质量。请记住,您正在回答未来读者的问题,这些人可能不知道您建议代码的原因。 - secelite
此代码旨在以年月日的形式获取确切的年龄。 - keerthi.rb
一个出生于1994年7月7日的人现在已经22岁4个月4天了。 - keerthi.rb
这段代码经过充分测试,可以适用于所有日期,包括闰年。 - keerthi.rb

-2

Dim d1 As Date

Dim d2 As Date

d1 = Format(dob.Value, "yyyy/MM/dd"

d2 = Format(System.DateTime.Now, "yyyy/MM/dd")

d = DateDiff(DateInterval.Year, d1, d2)'d-1提供准确的年龄


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接