Question

How can I code the bellow condition in VBScript?

>= 0 [Red] Ex.: {0,1,2,3,4...}
Between -1 and -7 [Yellow] Ex.: {-1,-2,-3,-4,-5,-6,-7} ONLY
Greater or equal than -8 [Green] Ex.: {-8,-9,-10,-11...}

I have the following code, the Mydate is a valid date and the Red part is OK. The problem is the Yellow, I don't know if I can you a range like I did. It looks like is getting ignored and yellowing a greater range instead.

<%
    IF DateDiff("d", MyDate, Now()) >= 0 THEN
%>
[Red]
<%
    ELSEIF DateDiff("d", MyDate, Now()) =< -1 OR DateDiff("d", MyDate, Now()) >= -8 THEN
%>
[Yellow]
<%
    ELSE
%>
[Green]
<%
    END IF
%>
Was it helpful?

Solution

When IFs get complicated, using Select Case may make things easier

For 'short' ranges; can handle 'exceptions' too:

Select Case DateDiff("d", dtA, dtB) ' computed just once automagically
  Case 1, 2, 3, 5  ' effectively OR without the noise => risk of messing up a complicated IF/ELSE/ELSEIF sequence
    ...
  Case 4, 6, 7, 1256
    ...
  Case Else
    ...
End Select

For 'large' continous ranges:

Dim nDiff : nDiff = DateDiff("d", dtA, dtB) ' computed just once
Select Case True ' <-- dirty? trick
  Case nDiff < -15
    ...
  Case nDiff < 0
    ...
  Case nDiff = 0
    ...
  Case nDiff < 11
    ...
  Case Else ' 11 or greater
End Select

now the logic is like snipping of parts of a number line/numerical ray.

PS:

Did you check your assumptions about DateDiff with code like:

>> dtB = Date()
>> dtA = DateAdd("d", -5, dtB)
>> WScript.Echo dtA, dtB, dateDiff("d", dtA, dtB)
>>
15.02.2014 20.02.2014 5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top