Results 1 to 6 of 6

Thread: [RESOLVED] Date Time today if then issues

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    Resolved [RESOLVED] Date Time today if then issues

    I am trying to compare a web form field with a date, and for some reason the compare never works. Here's the code:
    Code:
     
     Dim latepay As Decimal
            Dim Payment As Decimal = (total * 0.25)
            If Payment * 0.1 < "10" Then
                latepay = "10"
            Else : latepay = (Payment * 0.1)
            End If
            If total = "0" Then
                Payment = "2.00"
            End If
     If DDLQtr.SelectedValue = "1" And DateTime.Today.ToString("M") > "4" Then
                Payment = Payment + latepay
            ElseIf DDLQtr.SelectedValue = "1" And DateTime.Today.ToString("M") <= "4" Then
                Payment = Payment
            End If
            If DDLQtr.SelectedValue = "2" And DateTime.Today.ToString("M") > "7" Then
                Payment = Payment + latepay
            ElseIf DDLQtr.SelectedValue = "2" And DateTime.Today.ToString("M") <= "7" Then
                Payment = Payment
            End If
            If DDLQtr.SelectedValue = "3" And DateTime.Today.ToString("M") > "10" Then
                Payment = Payment + latepay
            ElseIf DDLQtr.SelectedValue = "3" And DateTime.Today.ToString("M") <= "10" Then
                Payment = Payment
            End If
            If DDLQtr.SelectedValue = "4" And DateTime.Today.ToString("M") > "1" Then
                Payment = Payment + latepay
            ElseIf DDLQtr.SelectedValue = "4" And DateTime.Today.ToString("M") <= "1" Then
                Payment = Payment
            End If
    The problem is, that regardless of the DDLQtr value chosen, the latepay is always applied. For example, if I choose DDLQtr value '3', and the DateTime.Today.ToString is '09/22/2016', should it not choose the elseif line to process? I have tried this with the "M", "MMM", and "MMMM" parsing, and nothing seems to work.

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Date Time today if then issues

    You aren't comparing dates: you are comparing strings. Is "Orange" greater than "Ferrari"?

    The Date object has various properties where you can get month, day, year, etc.

    So, you would have something like:

    Code:
    DateTime.Now.Month > 4
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    Re: [RESOLVED] Date Time today if then issues

    Thanks..datetime.now.tostring("MM") < blah..blah works now. For some reason datetime.today was not working.
    Thanks

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Date Time today if then issues

    The problem was not datetime.today, the problem was very definitely what SJWhiteley explained.

    The reason you have got it to work (or at least think you have) is not related to what you thought, but simply that you have altered the length of the string to enable string comparison to do what you expected.. but do you understand why?


    When you do String based comparisons, they are done in what is essentially alphabetical order... which means that "2" is considered to be greater than "10000" (or any other value that has "1" or "0" as a first character).

    Altering the length of the string means you are now getting a leading zero if apt (so instead of "9" you will get "09"), but whether or not that is valid depends on what you are comparing it to (eg: "09" is less than "7").


    Using strings means you need to be careful about every comparison to make sure you are doing what you wanted, and it also uses more CPU time and memory (but negligible amounts in most circumstances).

    If you use the numeric values as SJWhiteley showed, you do not have to worry about the length of text etc, as the comparison will be safe... your code will also be a bit shorter and easier to read, eg:
    Code:
            If DDLQtr.SelectedValue = "3" And DateTime.Now.ToString("MM") > "10" Then
            If DDLQtr.SelectedValue = "3" And DateTime.Now.Month > 10 Then

  5. #5
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: [RESOLVED] Date Time today if then issues

    Quote Originally Posted by dlhall View Post
    datetime.now.tostring("MM") < blah..blah works now. For some reason datetime.today was not working.
    I wouldn't expect it to work reliably since you are still converting portions of a date to a string instead of leaving it as integer to do your less than comparison.
    Code:
    DateTime.Now.Month > 4
    and
    Code:
    DateTime.Now.ToString("MM") > "4"
    are 2 very different things.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    Re: [RESOLVED] Date Time today if then issues

    You might be right..I did change to your recommendation..thanks again

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width