# Calculate age without IF statements or DateDiff

Page 1 of 1

## 0 Replies - 2132 Views - Last Post: 23 September 2010 - 05:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=191748&amp;s=233f8a69923211c52e25066f152d39bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jbdonnelly

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 23-September 10

# Calculate age without IF statements or DateDiff

Posted 23 September 2010 - 05:10 PM

Hi. I am having problems finding the age of a person using today's date (user input) and their birthday (also user input). I have created my code but it doesn't give the correct answer. I am having a problem getting an accurate age. Here is my code below:

```' Program:   Hours and Years Slept
' Author:    Jacquelyn Donnelly
' Date:      September 19, 2010
' Purpose:   This application calculates and displays
'            the total amount of hours slept in the user's
'            lifetime in years, months, days and hours.

Option Strict On

' How many days in a year - used in multiple procedures
Const _cintYearInDays As Integer = 360
' How many months in a year - used in multiple procedures
Const _cintYearInMonths As Integer = 12
' How many days in a month - used in multiple procedures
Const _cintMonthInDays As Integer = 30
' How many hours in a day
Const _cintDayInHours As Integer = 24
' How many hours slept in a day
Const _cintSleep As Integer = 8

Private Sub frmHoursandYearsSlept_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This even handler is executed when the form is loaded
' It displays the heading, clears the text property of the
' Total Years, Total Hours, Total Months and Total Days labels
' and sets the focus on the txtFirstName text box object.

Me.txtFirstName.Focus()
Me.lblTotalDays.Text = ""
Me.lblTotalHours.Text = ""
Me.lblTotalMonths.Text = ""
Me.lblTotalYears.Text = ""
Me.txtFirstName.Text = ""
Me.txtBirthDay.Text = ""
Me.txtBirthMonth.Text = ""
Me.txtBirthYear.Text = ""
Me.lblDaysSlept.Visible = False
Me.lblMonthsSlept.Visible = False
Me.txtCurrentDate.Text = ""
Me.txtCurrentMonth.Text = ""
Me.txtCurrentYear.Text = ""

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' This event handler is executed when the user clicks the
' Clear button. It clears the birth date text boxes, the
' first name text box, and the text property of the Total
' Hours, Total Months, Total Days, and Total Hours labels.
' Then, it sets the focus on the txtFirstName text box object.

Me.txtBirthDay.Clear()
Me.txtBirthMonth.Clear()
Me.txtBirthYear.Clear()
Me.txtFirstName.Clear()
Me.lblTotalHours.Text = ""
Me.lblTotalMonths.Text = ""
Me.lblTotalYears.Text = ""
Me.lblTotalDays.Text = ""
Me.txtFirstName.Focus()
Me.txtCurrentDate.Text = ""
Me.txtCurrentMonth.Text = ""
Me.txtCurrentYear.Text = ""

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' Close the programd and terminate the application

Me.Close()

End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' This event handler is executed when the user clicks the
' Calculate button. It calculates and displays the total
' amount of time slept in hours, days, months and years

Dim strFirstName As String
Dim strBirthDateMonth As String
Dim intBirthDateMonth As Integer
Dim srtBirthDateYear As String
Dim intBirthDateYear As Integer
Dim srtBirthDateDay As String
Dim intBirthDateDay As Integer
Dim srtTodayDateMonth As String
Dim intTodayDateMonth As Integer
Dim strTodayDateYear As String
Dim intTodayDateYear As Integer
Dim strTodayDateDay As String
Dim intTodayDateDay As Integer
Dim intYearsAlive As Integer
Dim intDaysAlive As Integer
Dim intHoursSlept As Integer
Dim intDaysSlept As Integer
Dim intMonthsSlept As Integer
Dim intYearsSlept As Integer
Dim intYearsToDays As Integer
Dim intMonthsToDays As Integer
Dim intTotalDaysAlive As Integer
Dim intMonthsAlive As Integer

strFirstName = Me.txtFirstName.Text
strBirthDateMonth = Me.txtBirthMonth.Text
intBirthDateMonth = Convert.ToInt32(strBirthDateMonth)
srtBirthDateDay = Me.txtBirthDay.Text
intBirthDateDay = Convert.ToInt32(srtBirthDateDay)
srtBirthDateYear = Me.txtBirthYear.Text
intBirthDateYear = Convert.ToInt32(srtBirthDateYear)
srtTodayDateMonth = Me.txtCurrentMonth.Text
intTodayDateMonth = Convert.ToInt32(srtTodayDateMonth)
strTodayDateDay = Me.txtCurrentDate.Text
intTodayDateDay = Convert.ToInt32(strTodayDateDay)
strTodayDateYear = Me.txtCurrentYear.Text
intTodayDateYear = Convert.ToInt32(strTodayDateYear)

intYearsAlive = intTodayDateYear - intBirthDateYear
intMonthsAlive = _cintYearInMonths - intBirthDateMonth
intDaysAlive = _cintYearInDays - intBirthDateDay
intYearsToDays = intYearsAlive * _cintYearInDays
intMonthsToDays = intMonthsAlive * _cintMonthInDays
intTotalDaysAlive = intDaysAlive + intYearsToDays + intMonthsToDays
intHoursSlept = intTotalDaysAlive * _cintSleep
intDaysSlept = intHoursSlept \ _cintDayInHours
intMonthsSlept = intDaysSlept \ _cintMonthInDays
intYearsSlept = intMonthsSlept \ _cintYearInMonths

Me.lblTotalDays.Text = intDaysSlept.ToString
Me.lblTotalMonths.Text = intMonthsSlept.ToString
Me.lblUserName.Text = strFirstName & "'s"

Me.lblDaysSlept.Visible = True
Me.lblMonthsSlept.Visible = True

End Sub
End Class

```

The program runs fine, but gives garbage as result. I am new at this and have been looking for a solution. We haven't covered IF statements yet. I have seen a lot of answers with DateDiff but we haven't gone over that either and I'm not sure how to implement it. If anyone could point me in the right direction I would very much appreciate it!

Thanks
Jacquelyn

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }