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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16874911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:56:01  来源:igfitidea点击:

compute age from given birthdate

vb.nettimestring-formatting

提问by joshua

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 txtyeara variable BODwill be equals to this.

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

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.

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

Can anyone help how to compute the age.

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

回答by KyleMit

There are really two questions here:

这里真的有两个问题:

  1. How to convert the string input into a DateTimeobject
  2. How to calculate age once you have your data in the correct format.
  1. 如何将输入的字符串转换为DateTime对象
  2. 一旦您的数据格式正确,如何计算年龄。

I'll let you follow other's instructions for how use TryParseExtractwhich is definitely the correct way to go here.

我会让你按照其他人的说明来使用TryParseExtract这绝对是正确的方法。

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

从他们的 DOB 确定某人的年龄时,请尝试使用以下方法:

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

这是杰夫阿特伍德非常受欢迎的问题如何计算某人的年龄的最佳答案的 vb 版本

I wrote a blogpost about calculating age from dobas well.

我也写了一篇关于从 dob 计算年龄的博客文章。

回答by tinstaafl

Here's a little different way using the year and month properties of the Date class:

下面是一种使用 Date 类的 year 和 month 属性的不同方式:

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

回答by user3122495

Here's a technique when you use Visual Studio 2012 VB.NET language

这是使用 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

回答by matzone

Use this function

使用这个功能

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

And apply it ..

并应用它..

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

回答by keerthi.rb

for complete age information use this code in c#.`

有关完整的年龄信息,请在 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;
    }

`

`

回答by Sujith Suseelan

Dim d1 As Date

Dim d1 作为日期

Dim d2 As Date

Dim d2 作为日期

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

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

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

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

d = DateDiff(DateInterval.Year, d1, d2)'d-1 provides accurate age

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