Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > To calculate a person's age by date, month and year in VB 2008
 

To calculate a person's age by date, month and year in VB 2008

Sphinx73,

I have seen your post, came to know your problem. I actually can't really help you because I am a beginner in VB 2008. But if you want you can help me from your experience in VB 2008 that you have successfully done the calculation to calculate a person's age by day, month and year. Which I am farntically looking for. I could not solve that problem in my VB project. meaning to say that I could not find outway to calculate a person's age by day, month and year.Which I could do in VB-6. If you don't mind. would you give me the project that you made in VB 2008 with VB code.You can send me a small little project of calculating a person's age by day, month and year and send me in mye-mail address, wasekm2001@yahoo.com. it will be highly appritiated. or any bodywho can help me in this regard. With many thanks. My Name isWasek from Bangladesh
WasekMajlis  Saturday, September 19, 2009 3:29 AM

Hi WasekMajlis,

Based on my understanding, you want to know how to get the age of a person with the year, month and day of his/her birthday. You can follow the code snippet below:

    Private Function GetAge(ByVal year As Integer, ByVal month As Integer, ByVal day As Integer) As Integer
        'Build the DateTime with the year,month and day.
        Dim birthday = New DateTime(year, month, day)
        'Subtract the year to get the age.
        Dim age As Integer = DateTime.Now.Year - birthday.Year
        Return age
    End Function


Let me know if this helps or not.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 21, 2009 10:07 AM

Hi WasekMajlis,

1. How to call the GetAge function.

For example, we have four TextBoxes and one Button on the form, these are their names and functions:
1) TextBoxYear : Used to enter year.

2)TextBoxMonth : Used to enter month.

3)TextBoxDay : Used to enter day.

4)TextBoxAge : Used to show the age.

5) ButtonShowAge : Click to show the age.

Then we add a click event handler as follows to show the age:
Private Sub ButtonShowAge_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles ButtonShowAge.Click

'Calculate the age.

Dim age = GetAge(Integer.Parse(TextBoxYear.Text), _

Integer.Parse(TextBoxMonth.Text), _

Integer.Parse(TextBoxDay.Text))

'Show the age

TextBoxAge.Text = Convert.ToString(age)

End Sub

You said: And one more thing, does it appriciate the leapYear and Month day fraction?

Based on my understanding, it is hard to decide the months of the age, but we can get the years and days of a age. For example, if my birthday is 1955-1-14, my age would be 54 years,254 days. This is the code snippet:

    Private Function GetAgeOfDays(ByVal year As Integer, ByVal month As Integer, ByVal day As Integer) As Integer
        'Build the DateTime with the year,month and day.
        Dim birthday = New DateTime(year, month, day)
        'Subtract the year to get the age.
        Dim age As Integer = (DateTime.Now - birthday).Days
        Return age
    End Function

    Private Sub ButtonShowAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowAge.Click
        'Calculate the age.
        Dim age = GetAgeOfDays(Integer.Parse(TextBoxYear.Text), _
                         Integer.Parse(TextBoxMonth.Text), _
                         Integer.Parse(TextBoxDay.Text))
        'Get years and days of the age
        Dim years As Integer = age / 365.25
        Dim days As Integer = age - years * 365.25
        'Adjust years and days if the years is too big and days become less than 0
        If (days < 0) Then
            years = years - 1
            days = age - years * 365.25
        End If
        'Show the age
        TextBoxAge.Text = Convert.ToString(years) & " years," _
                        & Convert.ToString(days) & " days"
    End Sub

2. How to calculate the sum of hours and miniutes.
We can get the text of each TextBox and convert to TimeSpan. Then we can sum them to get the summary. This is a code snippet:

    Private Sub ButtonSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSum.Click
        'Create the time span.
        Dim sum As New TimeSpan
        'Add the time one by one
        sum += GetTime(TextBox1.Text)
        sum += GetTime(TextBox2.Text)
        sum += GetTime(TextBox3.Text)
        'Show the time
        Dim minutes As Integer = (sum.Days * 24 + sum.Hours) * 60 + sum.Minutes
        Dim seconds As Integer = sum.Seconds
        TextBoxSum.Text = minutes & ":" & seconds
    End Sub

    Private Function GetTime(ByVal timeText As String) As TimeSpan
        Try
            'Split the string to seperate minutes and seconds
            Dim strArr As String() = timeText.Trim().Split(":")
            'Create time span based the minutes and seconds
            Dim minutes As Integer = Integer.Parse(strArr(0))
            Dim time As New TimeSpan(0, minutes, Integer.Parse(strArr(1)))
            Return time
        Catch ex As Exception
            Return TimeSpan.Zero
        End Try
    End Function

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 25, 2009 5:20 AM
Hi WasekMajlis,

Thanks for your reply. Please create a new thread to ask a new question.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byWasekMajlis Thursday, October 01, 2009 1:51 AM
  •  
Aland Li  Wednesday, September 30, 2009 2:04 AM

Hi WasekMajlis,

Based on my understanding, you want to know how to get the age of a person with the year, month and day of his/her birthday. You can follow the code snippet below:

    Private Function GetAge(ByVal year As Integer, ByVal month As Integer, ByVal day As Integer) As Integer
        'Build the DateTime with the year,month and day.
        Dim birthday = New DateTime(year, month, day)
        'Subtract the year to get the age.
        Dim age As Integer = DateTime.Now.Year - birthday.Year
        Return age
    End Function


Let me know if this helps or not.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 21, 2009 10:07 AM
Hi Aland Li, Thanks a lot,

But perhaps I did not mention that I am very basic in this business, You have given me the Function method which I do not know how to integret those in the form to view. Meaning to say that through textboxs or Labels ect in the form. I firmly believe that it is definitly going to work. If you kindly give me those tips. And one more thing, does it appriciate the leapYear and Month day fraction? If it does I will be more than happy.

Now I woul like to put forward on more problem that I am stuck with. I would like to mention that I am a Aviator, I mean Air Force Pilot. I take lot of interrest in VB 2008. Now, I would like to maintain my log book hours. I madea project but I failed to assimilate how to sum those hours and minutes in a form through Textboxs or Masked Textboxs. Let me give you some example.

Say Textbox1.text 50:30,
Textbox2.text 102:30,
Textbox2.text 202:30,
Textbox2.text 302:30,
Textbox2.text 1102:30, and so on

And the result Textbox should show the result in aggregate. Pl give me the tips considering me as a very beginner so that I can grasp the whole thing. Thanks again to hear from you.




WasekMajlis  Thursday, September 24, 2009 11:50 AM

Hi WasekMajlis,

1. How to call the GetAge function.

For example, we have four TextBoxes and one Button on the form, these are their names and functions:
1) TextBoxYear : Used to enter year.

2)TextBoxMonth : Used to enter month.

3)TextBoxDay : Used to enter day.

4)TextBoxAge : Used to show the age.

5) ButtonShowAge : Click to show the age.

Then we add a click event handler as follows to show the age:
Private Sub ButtonShowAge_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles ButtonShowAge.Click

'Calculate the age.

Dim age = GetAge(Integer.Parse(TextBoxYear.Text), _

Integer.Parse(TextBoxMonth.Text), _

Integer.Parse(TextBoxDay.Text))

'Show the age

TextBoxAge.Text = Convert.ToString(age)

End Sub

You said: And one more thing, does it appriciate the leapYear and Month day fraction?

Based on my understanding, it is hard to decide the months of the age, but we can get the years and days of a age. For example, if my birthday is 1955-1-14, my age would be 54 years,254 days. This is the code snippet:

    Private Function GetAgeOfDays(ByVal year As Integer, ByVal month As Integer, ByVal day As Integer) As Integer
        'Build the DateTime with the year,month and day.
        Dim birthday = New DateTime(year, month, day)
        'Subtract the year to get the age.
        Dim age As Integer = (DateTime.Now - birthday).Days
        Return age
    End Function

    Private Sub ButtonShowAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowAge.Click
        'Calculate the age.
        Dim age = GetAgeOfDays(Integer.Parse(TextBoxYear.Text), _
                         Integer.Parse(TextBoxMonth.Text), _
                         Integer.Parse(TextBoxDay.Text))
        'Get years and days of the age
        Dim years As Integer = age / 365.25
        Dim days As Integer = age - years * 365.25
        'Adjust years and days if the years is too big and days become less than 0
        If (days < 0) Then
            years = years - 1
            days = age - years * 365.25
        End If
        'Show the age
        TextBoxAge.Text = Convert.ToString(years) & " years," _
                        & Convert.ToString(days) & " days"
    End Sub

2. How to calculate the sum of hours and miniutes.
We can get the text of each TextBox and convert to TimeSpan. Then we can sum them to get the summary. This is a code snippet:

    Private Sub ButtonSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSum.Click
        'Create the time span.
        Dim sum As New TimeSpan
        'Add the time one by one
        sum += GetTime(TextBox1.Text)
        sum += GetTime(TextBox2.Text)
        sum += GetTime(TextBox3.Text)
        'Show the time
        Dim minutes As Integer = (sum.Days * 24 + sum.Hours) * 60 + sum.Minutes
        Dim seconds As Integer = sum.Seconds
        TextBoxSum.Text = minutes & ":" & seconds
    End Sub

    Private Function GetTime(ByVal timeText As String) As TimeSpan
        Try
            'Split the string to seperate minutes and seconds
            Dim strArr As String() = timeText.Trim().Split(":")
            'Create time span based the minutes and seconds
            Dim minutes As Integer = Integer.Parse(strArr(0))
            Dim time As New TimeSpan(0, minutes, Integer.Parse(strArr(1)))
            Return time
        Catch ex As Exception
            Return TimeSpan.Zero
        End Try
    End Function

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 25, 2009 5:20 AM
Aland Li,

You are great, I don't know how to express my gratitude to you, Had you been in my country probably I would request you for a dinner out side in a resturant. Anyway, your age tips is working fantastic. But the sum calculation, I did't try as yet. when I get back home after a meeting I will try and give you feed back. But one question? I only need to calculate the Hours and minutes not the seconds. Seeing your tips look like it caters for minutes and seconds only. Would you clarify the quries. And if you don't mind I will time and again disturb you for helping me in this regard. I have my believe that you will not regreat. By the way may I know where do you come from? with many thanks

Wasek here.
WasekMajlis  Friday, September 25, 2009 7:41 AM
Hi WasekMajlis,

You are welcome. I am glad that I can help you.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 25, 2009 9:43 AM
Hi Aland Li,

I worked as per your advice let me show what I have done

Public

Class Form1
Private Function GetTime(ByVal timeText As String) As TimeSpan

'Split the string to seperate minutes and seconds

sum.Add(GetTime(TextBox1.Text))

sum.Add(GetTime(TextBox2.Text))

sum.Add(GetTime(TextBox3.Text))

TextBoxSum.Text = sum.Minutes &

":" & sum.Seconds

End Sub

End

Class

But the TextBoxSum.Text is showing 0:0, I would like to remind you again That I only need ( Hours and Minutes SUM so that it shows, Say (1234:30) meaning 1234 Hrs 30 min.

Thanks Wasek, But U did not say where do you come from.

'Show the time
Dim sum As New TimeSpan

'Add the time one by one
Private Sub ButtonSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSum.Click

'Create the time span.
Dim time As New TimeSpan(0, Integer.Parse(strArr(0)), Integer.Parse(strArr(1)))

Return time

End Function
Dim strArr As String() = timeText.Trim().Split(":")

'Create time span based the minutes and seconds
WasekMajlis  Saturday, September 26, 2009 5:54 AM
Hi Aland Li,

Did you get my answer? Well if you have got Then I am expecting the answer. If not, Then I would like to say that the 2nd solution that u have given me that does not work, Shows "0:0" in the TextBoxSum.text. I only need for my project Hours and Minutes Sum which means

Say 345 Hrs 30 minWhich will be in the textbox (345:30)
20 hrs 45min ( 20:45)
1234 hrs 30 min (1234:30) And so on

the sum TextBox should be (1600:45)


WasekMajlis  Sunday, September 27, 2009 6:58 AM
Hi WasekMajlis,

Sorry for the late reply. I havetested the data you provided and modified the code snippet in my last reply. Please take alook at it.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 28, 2009 2:56 AM
Sorry, Aland

Looks like the last reply of yous did not come. I haven't got that one. would you pl send it again.

Regards,

Wasek
WasekMajlis  Monday, September 28, 2009 3:02 AM
Hi WasekMajlis,

I only modified the code, not did a new reply. Please read my reply posted at Friday, September 25, 2009 5:20 AM.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 28, 2009 3:04 AM
Aland Li,

You are grat, It is just working the way I wanted. Thanks a lot. Now can I put forward some more of my problem. I know you can solve it. I made a data base software, where I kept a provision of printing the form just one page. But by default the page set up is A4 with a 1/1 marginfixed. can you give me any clue so as to enable me to customize the page setting. Meaning to say that, I would like to set up the layout of the page and marging set up by my self. I used VB Pwoer pack 3.0 PrintForm componet to do that. Awaiting your reply.

Regards,

Wasek
WasekMajlis  Monday, September 28, 2009 2:10 PM
Hi WasekMajlis,

Thanks for your reply. Please create a new thread to ask a new question.

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byWasekMajlis Thursday, October 01, 2009 1:51 AM
  •  
Aland Li  Wednesday, September 30, 2009 2:04 AM
Hi Aland Li,

Hope you r fine. I don't have much thread knowledge. What I need now is, I have made a project of my Log book as per your advice making different fields of hours and minutes. It is working fine, if I want to add some new hours to a specific field I have to manuallycalculate old and new value and then put thatin the field as new value. What I want is? Is there any way, like a calculator, in a different page where I write the new valu in a text box with a button clickit will automatically be added to any specific old value. Not that I delete the old value and re-enter new value. Your suggestion is highly solicited. Regards

Wasek
WasekMajlis  Thursday, October 01, 2009 6:18 AM
Hi Aland Li,

  Perhaps you could not make any time to see my last post. However, I could solve that by my self. Tracking back to your earlier solution of the problem that you have given is just working very nice. But, I identified one  problem, Like. if I want to write thirty hours and thirty min it shows 30:30 it's just ok. But if I want to write three hours and five min after  clicking the sum  it shows 3:5. even if I put the input as 03:05 Where I need it to be displayed as 03:05 . if you kindly give me the solution. Regards

Wasek
WasekMajlis  14 hours 18 minutes ago
Hi WasekMajlis,

Sorry for the late reply. We need to modify the code below to add the extra zero.
        'Show the time
        Dim minutes As Integer = (sum.Days * 24 + sum.Hours) * 60 + sum.Minutes
        Dim seconds As Integer = sum.Seconds
        TextBoxSum.Text = minutes & ":" & seconds
This is the modified code snippet:
        Dim minutes As Integer = (sum.Days * 24 + sum.Hours) * 60 + sum.Minutes
        Dim seconds As Integer = sum.Seconds
        TextBoxSum.Text = String.Format("{0:D2}", minutes) & ":" & String.Format("{0:D2}", seconds)


Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  3 hours 23 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• How to compare 2 rows ieach from 2 different tables in c#
• ListView population and reusing existing listViewItems/SubItems
• Problems with AlwaysIncludeInCube
• DataGridView CurrentCell and SelectedRow
• Databinding not working when binding labels to a class' property
• Deleting a row from a gridview bound to a dataset
• Databinding fails to update bound control on first load
• inserting rownumbers in the Rowheader
• selected value of DataGrid
• Retrieving Images (BLOB) from a Dataset