Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i am a beginner in visual basic .net 2008 i want to ask something..

i have a 3 combo box which is intended for Month,Day and Year and three textboxes for the result and 1 button for the compute. the year is from 1950 upto 2012

i have some code but i is very hard coded because you must put it one by one.. anyone can help? thank you and God bless.

here is my sample code..

VB
 If ComboBox1.SelectedItem = "January" And ComboBox2.SelectedItem = "1" And ComboBox3.SelectedItem = "1950" Then
           
            TextBox1.Text = Val(29) - 1
            TextBox2.Text = (7)-1
            TextBox3.Text = Val(2012) - 1950

end if


which is the output is 62 years old 28 days and 6 months..
i want to make it simple and short..
end sub



how can i make my code short..
Posted
Updated 31-Jul-12 18:57pm
v2
Comments
enhzflep 1-Aug-12 1:05am    
Gday there,
Not sure about VB.NET, I'd have to check. With php and C, we have functions that can return time as an integer number of seconds that have elapsed since a particular day/date.
So, we can set day/month/year in human terms and get back an integer. We can then set a different data and get back a different integer. We can then simply subtract one from the other to get the amount of time elapsed in between them.

This is how, for instance CP forums may show a post as being made X seconds ago, or Y minutes ago, or Z days ago - or just simply as a date.

The functions also conveniently account for leap-years, etc.

I think it may even work like that in VB.NET.

Create a new date using the current time. Then subtract 7 from this date and set it to a new date. If the 2nd date = 1 week ago, then you're in business.

Yup, seems that it does work like that. Here's something I threw together quickly.
You'll want to checkout the help for DateTime and TimeSpan

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim format As String = "ddd d/MMM yyy HH:mm"
    Dim curTime As DateTime = DateTime.Now
    Dim birthDate As New System.DateTime(1900, 8, 8)
    Dim elapsed As TimeSpan


    TextBox1.Text = curTime.ToString(format)
    TextBox2.Text = birthDate.ToString(format)

    elapsed = curTime - birthDate

    TextBox3.Text = Int(elapsed.Days / 365.25)
End Sub


Displays:
Wed 1/Aug 2012 15:53
Wed 8/Aug 1900 00:00
111
 
Share this answer
 
Try this Code...
VB
Dim dob As DateTime
      dob = New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month,           DateTimePicker1.Value.Day)
       Dim tday As TimeSpan = DateTime.Now.Subtract(dob)
       Dim years As Integer, months As Integer, days As Integer
       months = 12 * (DateTime.Now.Year - dob.Year) + (DateTime.Now.Month - dob.Month)

       If DateTime.Now.Day < dob.Day Then
           months -= 1
           days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + DateTime.Now.Day
       Else
           days = DateTime.Now.Day - dob.Day
       End If
       years = Math.Floor(months / 12)
       months -= years * 12
       MsgBox("Your age as on " & Format(Now, "dd-MMM-yyyy") & vbCrLf & years & " Years, " & months & " Months and " & days & " Days")
 
Share this answer
 
v2
Comments
Alwafi220 22-May-14 15:42pm    
Thank You....
try this one.
VB
Sub calculateAge(ByVal birthDate As Date)
Dim mySpan As Int16 = CInt(Date.Now.Subtract(birthDate).TotalDays)
Dim nowYear As Int16 = Date.Now.Year
Dim nowMonth As Int16 = Date.Now.Month
Dim birthYear As Int16 = birthDate.Year
Dim birthMonth As Int16 = birthDate.Month

Dim yearCount As Int16
For yearCount = Date.Now.Year To birthYear Step -1
If yearCount Mod 4 = 0 Then
Select Case True
Case yearCount = nowYear And nowMonth < 3
'This is a leap year but we're not past Feb yet
'Do nothing
Case yearCount = birthYear And birthMonth > 2
'They were born after Feb in a leap year
'Do nothing
Case Else
'This was a full leap year, subtract a day to make it 365
mySpan -= 1
End Select
End If
Next

Dim myYears As Int16 = mySpan / 365
Dim myDays As Int16 = mySpan - (myYears * 365)
If myDays < 0 Then
myYears -= 1
myDays = 365 + myDays ' myDays is negative so this will subtract
End If

MessageBox.Show("You are " & myYears & " years and " & myDays & " days old.")
End Sub
 
Share this answer
 
hello gracia8xx

i would like to appear the result in texboxes..i have 3 textbox which is intended for the result.. the first textbox is for years old,the second is for month old,and the third one is for days old..




my design is 3 comboboxes which is the first cbobox is for month,second is for day,and the third is for the year and i have one button for calculating..



thanks a lot.. God bless
 
Share this answer
 
Comments
enhzflep 2-Aug-12 23:33pm    
ceryhel, you should add comments and remarks like this as a comment to a reply, not as a reply itself. Click the 'Have a Question or Comment' button at the bottom left of an answer to reply, or click "<-- Reply" on top right of a comment to do the same. As I suggested earlier, look in MSDN (or highlight in IDE and press f1) for help on DateTime and Timespan.
Here: DateTime and here: TimeSpan
As for your comment, you can see that the values all calculated and stored in mYears, mDays, you can work out how to get the months. Just display them in their own box, instead of gluing together as a string and showing in message box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900