Compare TextBox date to today

Discussion in 'ASP .Net' started by David C, Mar 27, 2009.

  1. David C

    David C 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
    David C, Mar 27, 2009
    #1
    1. Advertising

  2. David C

    imransyed63

    Joined:
    May 16, 2006
    Messages:
    27
    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()
    imransyed63, Mar 29, 2009
    #2
    1. Advertising

  3. David C

    Niall Guest

    Hi David,

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

    Niall
    Niall, Mar 31, 2009
    #3
  4. On Mar 27, 7:39 pm, "David C" <> wrote:
    > 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
    ....
    Alexey Smirnov, Mar 31, 2009
    #4
  5. David C

    David C Guest

    It was the value in the TextBox. Thanks.
    David
    "Alexey Smirnov" <> wrote in message
    news:...
    On Mar 27, 7:39 pm, "David C" <> wrote:
    > 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
    ....
    David C, Mar 31, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    5
    Views:
    371
    Patrick Steele [MVP]
    Jun 18, 2004
  2. TB
    Replies:
    0
    Views:
    439
  3. Replies:
    8
    Views:
    209
    peashoe
    Jan 24, 2005
  4. Vinnie Davidson

    ASP Date: get records with date = today (SQL Server)

    Vinnie Davidson, Aug 13, 2005, in forum: ASP General
    Replies:
    6
    Views:
    309
    Aaron Bertrand [SQL Server MVP]
    Aug 15, 2005
  5. joeyej
    Replies:
    7
    Views:
    127
    Randy Webb
    Apr 6, 2006
Loading...

Share This Page