שתף באמצעות


how to compare date in two textbox values

Question

Thursday, March 7, 2013 8:57 AM

In one textbox contains today's date and other contains previous date

compare today's date  greater then previous date

eg:   12/02/2013 > ( greater than ) 04/10/2011

using vb.net and sql server

All replies (7)

Thursday, March 7, 2013 11:17 AM ✅Answered | 2 votes

Midhous, 

There is no need to get the todays date from a Textbox. 

That is simply Now.Date

You have to convert the string which is in a textbox to a DateTime to convert but before that you have to check if it is a date, I use the VB function for that , other like more for this part typical C# code.

If IsDate(YourTextBox.Text) then
   If Now.Date > CDate(YourTextBox.Text) then
      'Do what you want to do
   end if
End if

Keep in mind that a date is very depending from the place and the settings of the computer which is in use. 

Main formats are
dd MM yyyy (All continents with exceptions bellow)
yyyy MM dd (In fact most former Communist nations plus some others)
MM dd yyyy (USA)

Success
Cor


Thursday, March 7, 2013 9:02 AM | 2 votes

Parse the strings and compare DateTime values. For example:

Dim date1 As DateTime = DateTime.Parse("12/02/2013")
Dim date2 As DateTime = DateTime.Parse("04/10/2011")
Dim is_greater As Boolean = date1 > date2

Note. Sometimes you will use Parse or TryParse that takes a specific culture.


Thursday, March 7, 2013 10:14 AM | 1 vote

Hi Midhous

Try This

'
 Dim Dat1 As DateTime = Me.TextBox1.Text
 Dim Dat2 As DateTime = Me.TextBox2.Text
'
 If Dat2 > Dat1 Then
    Me.Label1.Text = "Greater than..!"
 Else
    Me.Label1.Text = "Less than..!"
 End If
'

Regards


Thursday, March 7, 2013 11:19 AM

This is a good reason to have Option Strict On this style of coding leads to run time exceptions. Viorel_ shows (and explains about TryParse) how to get a date from a string. Personally I would use TryParse to be safe.

Enjoy life


Thursday, March 7, 2013 11:57 AM | 1 vote

Hello midhous,

In one textbox contains today's date and other contains previous date

compare today's date  greater then previous date

eg:   12/02/2013 > ( greater than ) 04/10/2011

using vb.net and sql server

can handle the comparison using the TryParse method, which also handles any exception at runtime if the date format is not valid unlike the parse method, here is a sample of the text boxes where you can enter the date.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim date1 As DateTime
        Dim date2 As DateTime
        Dim is_greater As Boolean


        If DateTime.TryParse(TextBox1.Text, date1) And DateTime.TryParse(TextBox2.Text, date2) Then
            is_greater = date1 > date2
            MessageBox.Show(is_greater.ToString)
        End If
    End Sub

Regards.


Thursday, March 7, 2013 9:48 PM

Instead of using a TextBox to get a date from the user, use a control designed for that purpose, such as a DateTimePicker.  Then you don't need to worry about format as much.


Friday, March 8, 2013 5:03 AM

@all

thank you boss for your valuable informations