Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 3, 2017 4:36 PM
I want to calculate age of person(In vb 2015) , for which I have one DateTimePicker in which I select Date of birth of person and a textbox(disabled for user) in which I want age in years automatically calculated and displayed.Can any one help.
All replies (8)
Sunday, February 5, 2017 2:36 PM âś…Answered | 1 vote
This is a ballpark value for years, but shows how the DateTimePicker.ValueChanged event can be used.
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
Dim ts As TimeSpan = DateTime.Now.Date - DateTimePicker1.Value
TextBox1.Text = String.Format("{0:n0} years old", (ts.TotalDays / 365))
End Sub
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
Friday, February 3, 2017 4:51 PM | 1 vote
How do I calculate a person's age with datetimepicker
Below function from link disable-enable of textboxes.
Public Shared Function GetCurrentAge(dateOfBirth As DateTime) As Integer
Dim now As DateTime = DateTime.Today
Dim years As Integer = now.Year - dateOfBirth.Year
If now.Month < dateOfBirth.Month OrElse (now.Month = dateOfBirth.Month AndAlso now.Day < dateOfBirth.Day) Then
years -= 1
End If
Return years
End Function
La vida loca
Friday, February 3, 2017 6:08 PM | 2 votes
I want to calculate age of person(In vb 2015) , for which I have one DateTimePicker in which I select Date of birth of person and a textbox(disabled for user) in which I want age in years automatically calculated and displayed.Can any one help.
There have been discussions here in the past about whether or not there's any such thing (can there be a partial year? What about leap years?)
I didn't try to answer that but the following - from a class that I made - gets the answer in pretty close terms. I'll use it to figure out how old I am <groan>:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load
Dim dd As DateDifference = _
DateDifference.GetDateDifference(#10/20/1958#, Now)
MessageBox.Show(dd.ToString)
Stop
End Sub
End Class
If you're interested then let me know and I'll post the code for the class. :)
"One who has no vices also has no virtues..."
Saturday, February 4, 2017 5:19 AM | 1 vote
Here is a fun way that's good for a few thousand years?
Public Class Form6
Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 1000, .Enabled = True}
Private NowLbl As New Label With {.Parent = Me, .Location = New Point(10, 20),
.Font = New Font("arial", 12, FontStyle.Bold), .AutoSize = True, .ForeColor = Color.AntiqueWhite}
Private dl As Double = 0 '= 1 when daylight savings
Private tz As Double = 7 - dl
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Text = "How Old Are You?"
BackColor = Color.Red
Size = New Size(400, 200)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim BirthDate As Date = #10/20/1958#
Dim NowDate As Date = Now
Dim jdayNow As Double = GetJulianDay(NowDate)
Dim jdayBirthday As Double = GetJulianDay(BirthDate)
NowLbl.Text = NowDate.ToLongDateString & " " & NowDate.ToLongTimeString & " TZ " & tz.ToString _
& vbLf & vbLf & "Julian Day:" & jdayNow.ToString("f5") & " GMT" _
& vbLf & vbLf & "Birth Day:" & BirthDate.ToShortDateString _
& vbLf & vbLf & "Age: " & ((jdayNow - jdayBirthday) / 365.25).ToString("f8") & " Years"
End Sub
Public Function GetJulianDay(ByVal thisDate As Date) As Double
Dim oday2 As Double = Int(thisDate.Day)
Dim year As Integer = Int(thisDate.Year)
Dim mon As Integer = Int(thisDate.Month)
If thisDate.Month = 1 Or thisDate.Month = 2 Then
year = Int(thisDate.Year - 1)
mon = Int(thisDate.Month + 12)
End If
Dim D As Double = (thisDate.Hour / 24.0) + (thisDate.Minute / 1440.0) + (thisDate.Second / 86400.0) + (tz / 24.0)
Dim B As Double = 2.0 - (Fix(year / 100.0)) + (Fix((Fix(year / 100.0)) / 4.0))
If thisDate.Year > 1582 Or thisDate.Month > 10 Then
B = B 'do nothing
ElseIf thisDate.Day < 15 Then
B = 0
End If
Return Math.Floor(365.25 * year) + Math.Floor(30.60001 * (mon + 1.0)) + oday2 + D + 1720994.5 + B
End Function
End Class
Sunday, February 5, 2017 2:18 PM
Yes you are right but my question is :I want to calculate age of person(In VISUAL BASIC 2015) , for which I have one DateTimePicker in which I select Date of birth of person and a textbox(disabled for user) in which I want age in years automatically calculated and displayed.Can any one help.
PLEASE DEMOSTRATE USING DateTimePicker and TextBox(disabled for user) (as soon as textbox GotFocus) . User neednot to enter value.
Sunday, February 5, 2017 10:05 PM
Yes you are right but my question is :I want to calculate age of person(In VISUAL BASIC 2015) , for which I have one DateTimePicker in which I select Date of birth of person and a textbox(disabled for user) in which I want age in years automatically calculated and displayed.Can any one help.
PLEASE DEMOSTRATE USING DateTimePicker and TextBox(disabled for user) (as soon as textbox GotFocus) . User neednot to enter value.
TextBox has ReadOnly property. There is no need to disable the TextBox.
DateTimePicker has ValueChanged event.
Plenty of code has been provided for you to use the DateTimePickers value changed event for providing the age of somebody in a TextBox. Although some effort would be required for that to occur I suppose.
La vida loca
Monday, February 6, 2017 3:54 PM
Thanks dbasnett.
Monday, February 6, 2017 4:31 PM
This is a ballpark value for years, but shows how the DateTimePicker.ValueChanged event can be used.
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged Dim ts As TimeSpan = DateTime.Now.Date - DateTimePicker1.Value TextBox1.Text = String.Format("{0:n0} years old", (ts.TotalDays / 365)) End Sub
"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein Multics - An OS ahead of its time.
You probably did not see that Dbasnett wrote Ballpark value.
But ,mr Monkeyboy gave us already a view on how the US Gov services works.
For those who want to calculate exactly the age of somebody, then don't use this sample alone. Only to see how the datetimepicker event works. But I cannot imagen somebody making windows desktop programs does not know that.
Success
Cor