Hi,

I am creating a project for my VB class, using VB 2008 Express, and I would like to be able to have the age of child (whose birthdate is a constant, in otherwords, it won't be changed or entered).

I figure that I would need to use the "Today subtract Birthdate" to give the answer in Years / Months / Days, but I can't figure out how to write it out.

I would have thought that other people would want to do something similar, but I haven't been able to find what I need.

Thanks for any help,
Sharon

Recommended Answers

All 4 Replies

Take a look at the datediff function, it's sole purpose is to calculate the difference between two dates ( thus it's name ).

Hi Sharon, not 100% sure if this is what you are after, but give it a go.

create yourself a form

add dateTimePicker control
add command button

add this code to the command button_on click event

MsgBox("Your birth date was " & DateTimePicker1.Text)
        MsgBox("Day of the year: " & _
          DateTimePicker1.Value.DayOfYear.ToString())

run the compiler and use the DateTimePicker to select DOB and click the command button.

Hope this helps

Use TimeSpan structure.

Dim d1 As Date = #12/5/2008#
        Dim d2 As Date = Now
        Dim ts As TimeSpan = d2 - d1
        Dim dt As Date = Date.MinValue + ts
        Console.WriteLine("Year : {0} Month : {1} Days : {2}", dt.Year, dt.Month, dt.Day)

This is what I have so far.

Dim TheDate As Date
        TheDate = #1/27/2008#

        Try
            TheDate = Date.Parse(txtDate.Text)
        Catch
            MessageBox.Show("Invalid date.")

        End Try

        Dim EnteredDate As Date

        EnteredDate = Date.Parse(txtDate.Text)

        Dim DaysTimeSpan As TimeSpan = Today.Subtract(EnteredDate)
        txtDifference.Text = DaysTimeSpan.Days.ToString()

Is there any "easy" way to get the output in a Years / Months / Days format?
I tried "adatapost"'s suggestion, but I wasn't sure how to make the answer show up in the text box.

Thanks again for the help,
Sharon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.