4

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.

Sam
  • 7,252
  • 16
  • 46
  • 65
joshua
  • 83
  • 2
  • 3
  • 7
  • 2
    This was one of the very first questions at SO, [question number 9](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age). Do make an effort to search for questions first, please. – Hans Passant Jun 01 '13 at 18:18
  • 1
    sorry sir. im new to programming. i've search for that but i dont understand most of the codes.. – joshua Jun 02 '13 at 14:34
  • Then you didn't ask your question in a very smart way. You are likely to just get more answers that you don't understand. That's pointless of course. Be sure to mention having found those answers and *explain* what you don't understand about them. Post the code you tried and say why it didn't do what you hoped it would do. – Hans Passant Jun 02 '13 at 14:45
  • 1
    Thank you sir for telling me about that. I will keep that in mind and make sure to that before asking a question. – joshua Jun 02 '13 at 15:23
  • Hans, actually, since question number 9 was for C#, and this is for VB, this isn't exactly the same question. That said, the question still could have been asked in a much better way. – ShoeMaker Mar 17 '15 at 17:43

6 Answers6

11

There are really two questions here:

  1. How to convert the string input into a DateTime object
  2. 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.

Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
3

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

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
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
1

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

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
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
0

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
matzone
  • 5,703
  • 3
  • 17
  • 20
  • This assumes 365 days in a year (every fourth has 366 - fail (365.25 would probably work most of the time)) and this assumes that there are exactly 30 days in every month (Feb has 28 (or 29), and there are multiple months with 31 - fail (30.45 is close enough most of the time)). The below answer is much better in these regards. – ShoeMaker Mar 17 '15 at 17:41
0

for complete age information use this code in 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;
    }

`

keerthi.rb
  • 91
  • 3
  • 9
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – secelite Nov 11 '16 at 12:53
  • This code is designed to get exact age in the formate age in year month and day – keerthi.rb Nov 11 '16 at 14:46
  • Example :- 22 years 4 month 4 days old. For a person who born 07/07/1994 – keerthi.rb Nov 11 '16 at 14:47
  • This code is tested well it will work for all date including leap year. – keerthi.rb Nov 11 '16 at 14:50
-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 provides accurate age