4

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

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

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

任何人都可以帮助如何计算年龄。

4

6 回答 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 版本如何计算某人的年龄

我还写了一篇关于从出生日期计算年龄的博文

于 2013-06-01T19:43:23.307 回答
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
于 2013-06-01T17:38:31.890 回答
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
于 2013-12-22T07:27:15.823 回答
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
于 2013-06-01T17:41:48.387 回答
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;
    }

`

于 2016-11-11T12:34:09.060 回答
-2

将 d1 调暗为日期

将 d2 调暗为日期

d1 = 格式(dob.Value,“yyyy/MM/dd”

d2 = 格式(System.DateTime.Now,“yyyy/MM/dd”)

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

于 2015-11-05T07:30:09.397 回答