Hi i have an assignment I’m working on. I have to let the user input their date of birth and then the program will print its age. I don’t know a lot about VB so if there is an easy way to do this. Thanks for the help.

From looking around on the site i got about this far

Sub Main()
        Dim Bday As Date
        Console.WriteLine("What is your date of birth?")
        Bday = Console.ReadLine
        Dim d1 As Date
        Dim d2 As Date
        d1 = Now
        d2 = Bday
        Dim iage As Integer
        iage = DateDiff(DateInterval.Year, d2, d1)
        Console.WriteLine(iage)
        Console.ReadKey()
    End Sub

It doesnt work right though cuz i enter my Bday as 6/30/1993
and it says im 18. Im actually about 17 and a half..
I changed it to

Dim Bday As Date
        Console.WriteLine("What is your date of birth?")
        Bday = Console.ReadLine
        Dim d1 As Date
        Dim d2 As Date
        d1 = Now
        d2 = Bday
        Dim iage As Integer
        Dim Years As Integer
        iage = DateDiff(DateInterval.Year, d2, d1)
        Years = iage - 1
        Console.WriteLine(Years)
        Console.ReadKey()

it seems to work like this

Hi,

You should do it like this:

Sub Main()
        Dim Bday As Date
        Console.WriteLine("What is your date of birth?")
        Bday = Console.ReadLine
        Dim d1 As Date
        Dim d2 As Date
        d1 = Now
        d2 = Bday
        Dim iage As Integer
        iage = DateDiff(DateInterval.Year, d2, d1) - 1
        Console.WriteLine(iage)
        Console.ReadKey()

    End Sub

You should do link this.

      1. Sub Main()
      2.  Dim Bday As Date
       3. Console.WriteLine("What is your date of birth?")
       4. Bday = Console.ReadLine
       5. Dim d1 As Date
       6. Dim d2 As Date
       7. d1 = Now
       8. d2 = Bday
       9. Dim iage As Integer
       10. iage = DateDiff(DateInterval.Year, d2, d1)
       11. Console.WriteLine(iage)
       12. Console.ReadKey()
      13  End Sub

Thanks !!!
View source

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.