[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.
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
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
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
Re: [RESOLVED] Date Time today if then issues
Quote:
Originally Posted by
dlhall
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.
Re: [RESOLVED] Date Time today if then issues
You might be right..I did change to your recommendation..thanks again