Compare TextBox date to today

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
 
Joined
May 16, 2006
Messages
27
Reaction score
0
Use,
Dim noOfDays As Long = DateDiff(DateInterval.Day, CDate(tb.Text), System.DateTime.Today())

noOfDays will be the date difference between tb.text & Today.

noOfDays < 0 ==> CDate(tb.Text) > System.DateTime.Today()
noOfDays > 0 ==> CDate(tb.Text) < System.DateTime.Today()
noOfDays = 0 ==> CDate(tb.Text) = System.DateTime.Today()
 
N

Niall

Hi David,

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

Niall
 
G

Guest

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
....
 
D

David C

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
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top