Compare TextBox date to today

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

I have the following code in my aspx page and I don't think the date
comparison is working and I need help making it work. The line that I think
is a problem is If CDate(tb.Text) < CDate(System.DateTime.Today()) Then

Dim tb As TextBox
Dim btn As Button
Dim row As FormViewRow = fvTickler.Row
If fvTickler.CurrentMode = FormViewMode.ReadOnly Then
tb = row.FindControl("txtExpireDate")
If (Not tb Is Nothing) Then
If tb.Text <> "" Then
If CDate(tb.Text) < CDate(System.DateTime.Today()) Then
'Tickler expired prior to today so make form
background red
fvTickler.RowStyle.BackColor = Drawing.Color.Red
End If
.....

Thanks.
David
 
Hi David,

Are the dates in the same format or are they getting converted to valid
datetimes??

Niall
 
I have the following code in my aspx page and I don't think the date
comparison is working and I need help making it work. The line that I think
is a problem is If CDate(tb.Text) < CDate(System.DateTime.Today()) Then

        Dim tb As TextBox
        Dim btn As Button
        Dim row As FormViewRow = fvTickler.Row
        If fvTickler.CurrentMode = FormViewMode.ReadOnly Then
            tb = row.FindControl("txtExpireDate")
            If (Not tb Is Nothing) Then
                If tb.Text <> "" Then
                    If CDate(tb.Text) < CDate(System.DateTime.Today()) Then
                        'Tickler expired prior totoday so make form
background red
                        fvTickler.RowStyle.BackColor = Drawing.Color.Red
                    End If
        .....

Thanks.
David

Your code looks correct, where do you mean it is "not working"? If
date is specified in a correct format than it should work. The only
thing is that you forget to check if format is correct. For example,
if date is "32/32/2008" you will get an exception. It means to avoid
this you need to add for example following code

Dim dateValue As Date
If Date.TryParse(tb.Text, dateValue) Then
If dateValue < System.DateTime.Today() Then
....
 
It was the value in the TextBox. Thanks.
David
I have the following code in my aspx page and I don't think the date
comparison is working and I need help making it work. The line that I
think
is a problem is If CDate(tb.Text) < CDate(System.DateTime.Today()) Then

Dim tb As TextBox
Dim btn As Button
Dim row As FormViewRow = fvTickler.Row
If fvTickler.CurrentMode = FormViewMode.ReadOnly Then
tb = row.FindControl("txtExpireDate")
If (Not tb Is Nothing) Then
If tb.Text <> "" Then
If CDate(tb.Text) < CDate(System.DateTime.Today()) Then
'Tickler expired prior to today so make form
background red
fvTickler.RowStyle.BackColor = Drawing.Color.Red
End If
.....

Thanks.
David

Your code looks correct, where do you mean it is "not working"? If
date is specified in a correct format than it should work. The only
thing is that you forget to check if format is correct. For example,
if date is "32/32/2008" you will get an exception. It means to avoid
this you need to add for example following code

Dim dateValue As Date
If Date.TryParse(tb.Text, dateValue) Then
If dateValue < System.DateTime.Today() Then
....
 
Back
Top