Elliot Morgan Elliot Morgan - 1 year ago 124
Vb.net Question

How to calculate how many days until someone will turn 18 using their date of birth in VB?

For a computer science assignment I need to calculate how many days it will be until the user turns 18, using their date of birth.

Dim dateToday As Date = Date.Today()

Dim ageNow As Integer = dateToday.Subtract(birthDate).Days

' 6750 is 365 * 18
Dim daysEighteen As Integer = 6750 - ageNow

MsgBox("You will be 18 in " & daysEighteen)

This is the code I have at the moment, however when tested it doesn't come up with the correct output.

This isn't the entire code but I have just included the necessary lines for this part of the program.

Thanks in advance.

Answer Source

I would use a different method. Add 18 years to the birthdate and then subtract the today date. This should avoid the error implicit in your calculation (365*18 doesn't count the leap years)

Dim birthDate = New DateTime(1999,1,1)
Dim adultDate = birthDate.AddYears(18)
Dim daysLeft = adultDate.Subtract(DateTime.Today).Days
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download