converting datetimepicker value

i had a datetimepicker named dtpBirthDate, and want to use this as a meduim for obtaining birthdate of a user, what i want to happen exactly is upon choosing the date, three(3) textboxes namely, tboxYears, tboxMonths,tboxDays would be fill up by the number of years,months,and days the user was.
here's the code:

Private Sub dtpBirthDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBirthDate.ValueChanged
Dim dtpReg As DateTime = Me.dtpRegDate.Value
Dim tStart As DateTime = Me.dtpBirthDate.Value
Dim tFinish As DateTime = dtpReg

Dim TotalTime As TimeSpan = tFinish.Subtract(tStart)
Dim dd As Double = TotalTime.TotalDays

Me.tboxYears.Text = ((TotalTime.TotalDays) / 365.25).ToString
Me.tboxMonths.Text = (((TotalTime.TotalDays) / 365.25) * 12).ToString
Me.tboxDays.Text = TotalTime.TotalDays.ToString

End Sub

problem is i really don't know how to play with this so that the textboxes will display the way i wanted to be...one thing is how can i convert them so that it will show in whole number.thanks...


Answer this question

converting datetimepicker value

  • Eder F. Dias

    thanks for your response but i really need the actual revision of the code....

  • basedissonance

    thanks Mark, this is what exactly i am looking for.....

  • Norbert Eder

    1 - you're calling tostring THEN subtracting a number. This means VB converts it to a string, as instructed, magically turns it back into a number, then magically turns it back into a string.

    2 - You're subtracting the number of years past, but not the number of months. As a result, you'll get too many days. I'm not getting negative results here tho.

    3 - Your use of .25 will still result in rounding errors at times, I would expect. But only if you go back 3 years without going back over a leap year, or if you go back <3 years and one was a leap year.

    4 - you still have old code that you're not using in there.

    5 - DateDiff will return 1 if your span crosses a year, even if the number of days < 365.25. Therefore, this approach can give negative values, as you're finding. According to the docs, a diff between Dec 31 and Jan 1 of the following year gives a return value of one year, even though only a day has passed.



  • Chrismanster

    I think his should do it for you

    Dim Firstdate As Date

    Dim Lastdate As Date

    Dim years As Integer

    Dim months As Integer

    Dim days As Integer

    ' the value of your datetime picker should go here

    Firstdate = New Date(1990, 1, 1)

    Lastdate = Now

    years = Lastdate.Year - Firstdate.Year

    months = Lastdate.Month - Firstdate.Month

    days = Lastdate.Day - Firstdate.Day

    ' starting with the days, we need to make sure we are counting full months and years

    If days < 0 Then

    ' need to subtract a month from months

    ' don't do anything with the days yet

    months -= 1

    End If

    If months < 0 Then

    ' subtract a year

    years -= 1

    End If

    ' we now have the correct number of years - almost!

    ' last check for years

    If months = 0 And days < 0 Then

    ' same month - haven't completed it though (e.g. 21 May 1990 - 19 -May 2006

    years -= 1

    End If

    'correct number of years

    ' move on to months

    If months < 0 Then

    ' get the correct number of months (this statement could have been added above - just down here for clarity)

    months = months + 12 ' e.g. - 5 months = subtract 1 year (done) and add 12 months

    End If

    If days < 0 Then

    ' need to get the actual number of days

    ' get the number of days in the month (for the lastest date)

    ' add then to the days value ( e.g. -5 + 31 for may)

    ' this will also take Feb in leap years into account

    days = Date.DaysInMonth(Lastdate.Year, Lastdate.Month) + days

    End If

    ' I think you've now got your years,months and days


  • Tracey_nz

    i just now use datediff and it was doing fine for the years, but not for months and days since it returns a negative values..i know it is in my code and i hope someone out there coud figure it out for me...thanks.

    Dim tStart As DateTime = Me.dtpBirthDate.Value
    Dim tFinish As DateTime = Now()

    Dim TotalTime As TimeSpan = tFinish.Subtract(tStart)
    Dim dd As Integer = TotalTime.TotalDays
    Dim tYears = DateDiff(DateInterval.Year, tStart, tFinish).ToString
    Me.tboxYears.Text = tYears
    Me.tboxMonths.Text = DateDiff(DateInterval.Month, tStart, tFinish).ToString - (tYears * 12)
    Me.tboxDays.Text = DateDiff(DateInterval.Day, tStart, tFinish).ToString - (tYears * 365.25)

  • Cornel Gav

    Not from me. Math.Round solves the immediate problem, but you need to code it completely differently if you want accurate results.



  • Patrice RAUCQ

    thanks once again for being so prompt, I know exactly that it was terribly wrong but unfortunately, I was definitely new in programming and VB.net in particular, so I was seeking for a desent solution for my problem here...I know someone out there could possibly help me...

  • GregDD

    anymore inputs

  • VFP Developer

    Doesn't the TimeSpan class have properties for days and months, as well as years



  • Skynyrd

    I think this will do what you had in mind:

    Dim ts As TimeSpan = TimeSpan.FromDays(420)

    Dim years As Integer

    Dim months As Integer

    Dim days As Double = ts.Days

    years = Math.Floor(days / 365.25)

    days -= years * 365.25

    months = Math.Floor(days / 30)

    days -= months * 30

    days = Math.Round(days);

    But it's not going to be accurate. The only way to do this accurately is to do the math yourself on the two datetime objects. You simply cannot guess the number of days in the month and expect to report something that is 100% accurate. You also can't use 365.25 on the basis that it works out over 4 years, it is an off by one error waiting to happen.

    I guess that's why timespan doesn't have years and months, because it's not anchored to any specific time period. That was my mistake, sorry.



  • converting datetimepicker value