Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datetimepicker named (DTPEntryED) and textbox named (TxtLengthoSED). when i run the program and select any date for example 24/01/2012 and want to know the difference between that date and today i.e 15/07/2022 it gives me a result as (10 years, 126 months, 7671 days).

Kindly someone guide me where i went wrong in my code and how to get the correct result i.e (10 years, 05 months, 20 days).

Remember my project is in VB.Net


What I have tried:

Private Sub DTPEntryED_ValueChanged(sender As Object, e As EventArgs) Handles DTPEntryED.ValueChanged

    Dim yearValue As Integer
    Dim monthValue As Integer
    Dim dayValue As Integer
    yearValue = Math.Abs(DateDiff("yyyy", DTPEntryED.Value, Today))
    monthValue = Math.Abs(DateDiff("m", DTPEntryED.Value, DateAdd("y", yearValue, Today)))
    dayValue = Math.Abs(DateDiff("d", DTPEntryED.Value, DateAdd("m", monthValue, DateAdd("y", yearValue, Today))))
    TxtLengthoSED.Text = Format(yearValue, "00") & " years, " _
    & Format(monthValue, "00") & " months, " _
    & Format(dayValue, "00") & " days"

End Sub
Posted
Updated 14-Jul-22 19:49pm

1 solution

Ignore Maths, and DateDiff: use a TimeSpan[^]
You are mixing up very old VB (VB6 and earlier!) constructs with the newer .NET versions, which is never a good idea.

If you subtract two DateTime values, you get a Timespan:
VB
Dim dt As DateTime = New DateTime(2022, 7, 15)
Dim diff As TimeSpan = dt - DateTime.Now
Console.WriteLine(diff)
The TimeSpan struct includes a Duration method, which returns the absolute value of a Timespan:
VB
Dim dt As DateTime = New DateTime(2022, 7, 15)
Dim diff As TimeSpan = (dt - DateTime.Now).Duration()
Console.WriteLine(diff)
 
Share this answer
 
Comments
Ali Khan 3 15-Jul-22 2:13am    
Dear OriginalGriff can you please send the complete code for my datetimepicker.valuechanged event as i am very new in programming and can't fully understand how cant get the result in my textbox using the above code you told me... Thanks
OriginalGriff 15-Jul-22 3:41am    
Certainly! Where would you like me to send the pro forma invoice?

This is not a "do my code for me" service: while we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

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