none
When DateTimePicker Value Changed This Error Occured RRS feed

  • Question

  • Hi Experts,

    When DateTimePicker Value Changed This Error Occured

    I'm using this code to calculate the age when DateTimePicker value changed this snippet code was working in a good manner but suddenly it makes error  

     Private Sub DTPicker_ValueChanged(sender As Object, e As EventArgs) Handles DTPicker.ValueChanged
    
            With DTPicker.Value
                Dim x As DateTime
                x = New DateTime(Now.Year, .Month, .Day)
                Dim ageY As Integer = (Now.Year - .Year)
                Dim ageM As Integer = (Now.Month - .Month)
                'Dim ageD As Integer = (Now.Day - .Day)
                If x > Now Then ageY -= 1
                If x > Now Then ageM += 12
                'If x > Now Then ageD += 30
                txtAge.Text = CStr(ageY & " Y " & " , " & ageM & " M ")
            End With
        End Sub



    • Edited by Amr_Aly Sunday, October 22, 2017 8:25 PM
    Sunday, October 22, 2017 8:00 PM

Answers

  • Hello,

    Here is a code sample to consider. I placed a class for doing ages in the form but would suggest if you like the code place the class into it's own class file.

    Note each of the read-only properties are available to you besides the one shown used below.

    Public Class Form1
        Private operations As DateTimeCalculations = New DateTimeCalculations
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            operations.ToAgeString(DateTimePicker1.Value)
            Label1.Text = operations.Formatted
        End Sub
    End Class
    Public Class DateTimeCalculations
        Public Sub ToAgeString(ByVal dob As Date)
            Dim today As Date = Date.Today
    
            Dim months As Integer = today.Month - dob.Month
            Dim years As Integer = today.Year - dob.Year
    
            If today.Day < dob.Day Then
                months -= 1
            End If
    
            If months < 0 Then
                years -= 1
                months += 12
            End If
    
            Dim days As Integer = (today - dob.AddMonths((years * 12) + months)).Days
    
            mMonths = months
            mDays = days
            mYears = years
    
            mFormatted = String.Format("{0} year{1}, {2} month{3} and {4} day{5}",
                                       years, If(years = 1, "", "s"),
                                       months, If(months = 1, "", "s"),
                                       days, If(days = 1, "", "s"))
    
        End Sub
        Private mFormatted As String
        Public ReadOnly Property Formatted As String
            Get
                Return mFormatted
            End Get
        End Property
        Private mMonths As Integer
        Public ReadOnly Property Months As Integer
            Get
                Return mMonths
            End Get
        End Property
        Private mDays As Integer
        Public ReadOnly Property Days As Integer
            Get
                Return mDays
            End Get
        End Property
        Private mYears As Integer
        Public ReadOnly Property Years As Integer
            Get
                Return mYears
            End Get
        End Property
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Amr_Aly Sunday, October 22, 2017 10:08 PM
    Sunday, October 22, 2017 9:26 PM
    Moderator

All replies

  • Hi

    Please DO NOT POST CODE as an Image. Do you really expect others to retype your code?


    Regards Les, Livingston, Scotland

    Sunday, October 22, 2017 8:04 PM
  • Hi

    Please DO NOT POST CODE as an Image. Do you really expect others to retype your code?


    Regards Les, Livingston, Scotland

    Hi Leshay ,

    If you wait for some few minutes you were find my code,thanks for clarification 

    Sunday, October 22, 2017 8:09 PM
  • Hi

    Thank you for reformatting your post.

    If you just need Year and Month for age then this may help.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            Dim s As String = Nothing
            Dim y As Long = DateDiff(DateInterval.Year, DateTimePicker1.Value, Now)
            Dim m As Long = DateDiff(DateInterval.Month, DateTimePicker1.Value, Now) - y * 12
            If Not m = 1 Then s = "s"
            Label1.Text = y & " years " & m & " month" & s
        End Sub End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, October 22, 2017 9:30 PM Changed to use DTP changed event
    Sunday, October 22, 2017 8:57 PM
  • Hello,

    Here is a code sample to consider. I placed a class for doing ages in the form but would suggest if you like the code place the class into it's own class file.

    Note each of the read-only properties are available to you besides the one shown used below.

    Public Class Form1
        Private operations As DateTimeCalculations = New DateTimeCalculations
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            operations.ToAgeString(DateTimePicker1.Value)
            Label1.Text = operations.Formatted
        End Sub
    End Class
    Public Class DateTimeCalculations
        Public Sub ToAgeString(ByVal dob As Date)
            Dim today As Date = Date.Today
    
            Dim months As Integer = today.Month - dob.Month
            Dim years As Integer = today.Year - dob.Year
    
            If today.Day < dob.Day Then
                months -= 1
            End If
    
            If months < 0 Then
                years -= 1
                months += 12
            End If
    
            Dim days As Integer = (today - dob.AddMonths((years * 12) + months)).Days
    
            mMonths = months
            mDays = days
            mYears = years
    
            mFormatted = String.Format("{0} year{1}, {2} month{3} and {4} day{5}",
                                       years, If(years = 1, "", "s"),
                                       months, If(months = 1, "", "s"),
                                       days, If(days = 1, "", "s"))
    
        End Sub
        Private mFormatted As String
        Public ReadOnly Property Formatted As String
            Get
                Return mFormatted
            End Get
        End Property
        Private mMonths As Integer
        Public ReadOnly Property Months As Integer
            Get
                Return mMonths
            End Get
        End Property
        Private mDays As Integer
        Public ReadOnly Property Days As Integer
            Get
                Return mDays
            End Get
        End Property
        Private mYears As Integer
        Public ReadOnly Property Years As Integer
            Get
                Return mYears
            End Get
        End Property
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Amr_Aly Sunday, October 22, 2017 10:08 PM
    Sunday, October 22, 2017 9:26 PM
    Moderator
  • Amr,

    I don't get that error? It seems to work as is? There must be some other issue. Put a break on the line and look at the values. Single step and make sure it does what you think on each line. The values are what you expect.

    Watch for years with dates like Feb 29 or crossing midnight new years eve etc.

    Sunday, October 22, 2017 10:00 PM
  • Thank you Karen ,

    It's so helpful code, I've searched it for a long time 

    Many thanks Karen , See you in another thread 


    Regards From Amr_Aly

    Sunday, October 22, 2017 10:07 PM
  • Amr,

    I don't get that error? It seems to work as is? There must be some other issue. Put a break on the line and look at the values. Single step and make sure it does what you think on each line. The values are what you expect.

    Watch for years with dates like Feb 29 or crossing midnight new years eve etc.

    Hi Tommy,

    The problem is when i changed the time to 29/2/yyyy=>(any year) the message above appear.

    After i asked my question i tried to use Break point but i can't use it as ought

    Thanks Tommy for a good hint 


    Regards From Amr_Aly

    Sunday, October 22, 2017 10:26 PM
  • The problem is when i changed the time to 29/2/yyyy=>(any year) the message above appear.

    Feb 29 only exits every four years in the proper leap years. So you get an error when you enter one that is not valid a leap day in a leap year.

    So if you enter Feb 29, 2008 on the date time picker, you then try to get the now date for Feb 29, 2017 but that is not a valid day. 2017 is not leap year.

    You will also have problems when the dates are across the new year with your code. ie if the then date was dec and now date is jan, I dont think your code handles that correctly?

    It appears Karen's example can help. See how its adjusting for the months that go across dec/jan?

    You will also need to make adjustments if your dates are to be beyond say 500 years depending on what you need it for. And you need to watch the cultures as some have different calendars etc. So depends on your needs and what the dates are.

    Karen's method will develop errs beyond a few hundred years I believe though I did not actually test it since for most it does not matter.

    Just so you know. :)


    Monday, October 23, 2017 8:44 AM
  • The problem is when i changed the time to 29/2/yyyy=>(any year) the message above appear.

    Feb 29 only exits every four years in the proper leap years. So you get an error when you enter one that is not valid a leap day in a leap year.

    So if you enter Feb 29, 2008 on the date time picker, you then try to get the now date for Feb 29, 2017 but that is not a valid day. 2017 is not leap year.

    You will also have problems when the dates are across the new year with your code. ie if the then date was dec and now date is jan, I dont think your code handles that correctly?

    It appears Karen's example can help. See how its adjusting for the months that go across dec/jan?

    You will also need to make adjustments if your dates are to be beyond say 500 years depending on what you need it for. And you need to watch the cultures as some have different calendars etc. So depends on your needs and what the dates are.

    Karen's method will develop errs beyond a few hundred years I believe though I did not actually test it since for most it does not matter.

    Just so you know. :)



    With no disrespect, I doubt hundreds of years will be an issue if we are being realistic unless I'm missing something.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 23, 2017 5:22 PM
    Moderator