[2005] Date Format - TROUBLE
In my for i have a bounded TextBox (txt_pana_la_data.Text).
I want to compare the txt_pana_la_data.Text with the curent date in the format "dd/MM/yyyy".
I have tryed with this code but is not working if i have for example the txt_pana_la_data.Text = 25/10/2007... what's wrong ?
Code:
Private Sub txt_pana_la_data_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_pana_la_data.TextChanged
'Dim data_curenta = Date.Now
Dim data_expirarii As Date
Dim data_curenta As Date
data_curenta = Today
Format(data_curenta, "dd/MM/yyyy")
data_expirarii = Me.txt_pana_la_data.Text
Format(data_expirarii, "dd/MM/yyyy")
If data_expirarii < data_curenta Then
Me.txt_stare_permis.Text = "Expirat"
Me.btn_prelungire.Enabled = True
Me.Validate()
Me.permis_BindingSource.EndEdit()
Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
Me.permis_BindingSource.Clear()
Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
Else
'Me.txt_stare_permis.Text = "Activ"
End If
End Sub
Re: [2005] Date Format - TROUBLE
Is there a reason that you chose to use a TextBox instead of the DateTimePicker, which is designed for dates?
If you do want to stick with a TextBox then you can't use the '<' operator on strings because it will compare them alphabetically. If you want to compare dates then you have to compare Date objects:
vb.net Code:
Dim input As Date
If Date.TryParseExact(Me.TextBox1.Text, _
"dd/MM/yyyy", _
Nothing, _
Globalization.DateTimeStyles.None, _
input) Then
If input < Date.Today Then
'The specified date is before today's date.
End If
Else
'Invalid input.
End If
Note also that if you specify "dd/MM/yyyy" as the format the the user will have to include leading zeroes on single-digit days. It might be more appropriate to use "d/MM/yyyy", which will accept single- or double-digit days.
Re: [2005] Date Format - TROUBLE
The TextBox is ReadOnly... user does not type a date.
My computer date is 25.01.2008 and i have into the txt_pana_la_data.text the value 10/12/2007 and it's not working! Why ?!?!
This code i've used!
Code:
Dim input As Date
If Date.TryParseExact(Me.txt_pana_la_data.Text, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, input) Then
If input < Date.Today Then
Me.txt_stare_permis.Text = "Expirat"
Me.btn_prelungire.Enabled = True
Me.Validate()
Me.permis_BindingSource.EndEdit()
Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
Me.permis_BindingSource.Clear()
Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
End If
Else
'Invalid input.
End If
Re: [2005] Date Format - TROUBLE
If your TextBox is ReadOnly then why do you need to test the value in the TextBox at all? That value must have come from somewhere, and that somewhere is presumably a Date value. You should be testing THAT value against the current Date.
Re: [2005] Date Format - TROUBLE
The value comes from the database there where is stored as Text. I have more textboxes and a DGV that are bounded to a BindingSource. I have to display it (thru BindingSource - It's OK with this), read it and if it's smaller than the current date to display into another TextBox the value = "Expirat" (expired). (that's the point that i don't pass forward).
I have used your code but seems that is not working! Why... what i am doing wrong ????
I read a text value from database, i transform it to a date.today... (dd/MM/yyyy) and i compare it with the current date transformed to dd/MM/yyyy too.
Re: [2005] Date Format - TROUBLE
Why are you reading a text value from the database? If it's a date then why is it not stored as a date? Do you store numbers as text or as numbers? If you store numbers as numbers then why don't you store dates as dates?
Also, my code does what it's supposed to do. How would I know what you're doing wrong if you haven't shown me what you are doing?
Re: [2005] Date Format - TROUBLE
http://www.mbm-hosting.com/sample_screen.png
Code:
Private Sub frm_gestionare_permise_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Permis_DataSet.lista_abonati' table. You can move, or remove it, as needed.
Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
End Sub
Private Sub txt_pana_la_data_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_pana_la_data.TextChanged
Dim input As Date
If Date.TryParseExact(Me.txt_pana_la_data.Text, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, input) Then
If input < Date.Today Then
Me.txt_stare_permis.Text = "Expirat"
Me.btn_prelungire.Enabled = True
Me.Validate()
Me.permis_BindingSource.EndEdit()
Me.permis_TableAdapter.Update(Me.permis_DataSet.lista_abonati)
Me.permis_BindingSource.Clear()
Me.permis_TableAdapter.Fill(Me.permis_DataSet.lista_abonati)
End If
Else
'Invalid input.
End If
End Sub
Re: [2005] Date Format - TROUBLE
Your txt_pana_la_data TextBox is bound to a column from your DataTable. That columns should contain Date values and you should be comparing that data to the current date. You should not be parsing the string in the TextBox and your DataColumn should not contain strings.