Click here to Skip to main content
14,427,102 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need to find difference between two dates in textboxes. the difference should be in years, months and dates.
I try to use Datediff but it didn't works with months and days
Dim y_diff As Integer
     Dim m_diff As Integer
     Dim d_diff As Integer
     Dim date1 As Date = Convert.ToDateTime(Textbox1.Text)
     Dim dob As Date = Convert.ToDateTime(TextBox2.Text)
     Response.Write("Today date is " & Today.Date & "<br>")
     y_diff = DateDiff("yyyy", dob, date1)

     m_diff = DateDiff("m", dob, date1) / y_diff / 12

     d_diff = DateDiff("d", dob, date1) / 365
     Response.Write("Difference in year" & y_diff & "<br>" & "months" & m_diff & "<br>" & d_diff & "<br>")

how can i get it correctly?
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-14 21:31pm
   
What's wrong with reading original MSDN documentation?
—SA
Sergey Alexandrovich Kryukov 30-May-14 21:54pm
   
And please, stop posting "solutions" which are not solution. This is considered as abuse.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't use Date, use System.DateTime. How about difference? Great secret: using the operator '-' :-).
And don't use Convert. Even when it works, essentially this is not "converting". Use one of the methods called Parse, ParseExact, TryParse or TryParseExact:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

The subtraction operator returns an object of the type System.TimeSpan, so see also:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^],
http://msdn.microsoft.com/en-us/library/ae6246z1.aspx[^].

Even better, instead of those text boxes, use some type of date-time picker, something like
TextBox allow only (HH:mm tt) format using asp.net[^].

—SA
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Learn from this example:
Imports System
				
Public Module Module1
	Public Sub Main()
		Dim text1 As String = "2014-5-24"
		Dim text2 As String = "2014-5-30"
		Dim datetime1 As DateTime
		Dim datetime2 As DateTime
		If DateTime.TryParse(text1, datetime1) And DateTime.TryParse(text2, datetime2)  Then
			Dim difference As TimeSpan = datetime2.Subtract(datetime1)
			Console.WriteLine("{0} Days", difference.TotalDays)
        	Console.WriteLine("{0} Hours", difference.TotalHours)
        	Console.WriteLine("{0} Minutes", difference.TotalMinutes)
       		Console.WriteLine("{0} Seconds", difference.TotalSeconds)
		Else
			Console.WriteLine("Date time is invalid")
		End If
			Console.ReadLine()
	End Sub
End Module

However, you should consider using some datepicker control like jquery datepickers instead of textboxes which allow free input from users. Datepicker will return datetime value so no need to convert.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100