2014-02-20 66 views
0

如何在VBScript中編寫波紋管條件?使用Datediff查找範圍值

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

我有以下代碼中,指明MyDate是一個有效的日期和紅色部分是確定。問題是黃色,我不知道我是否可以像我一樣的範圍。它看起來像是被忽略,而是泛黃更大的範圍。

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

回答

5

當國際單項體育聯合會變得複雜,使用選擇事例可能使事情變得更容易

對於「短」的範圍;可以處理「例外」太:

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 

對於「大」連續範圍:

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 

現在邏輯就像是一個數字線路/數字射線的部件剪斷。

PS:

你檢查你對DateDiff的假設有如下代碼:

>> dtB = Date() 
>> dtA = DateAdd("d", -5, dtB) 
>> WScript.Echo dtA, dtB, dateDiff("d", dtA, dtB) 
>> 
15.02.2014 20.02.2014 5 
+0

+1的使壞(我個人使用,所有的時間)。 – Lankymart

+0

感謝您的提示! – Khrys