使用Datediff查找范围值

时间:2014-02-20 14:15:53

标签: vbscript asp-classic datediff

如何在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...}

我有以下代码,Mydate是一个有效的日期,Red部分是OK。问题是黄色,我不知道我能不能像你一样做范围。它似乎被忽略了,而且变黄了更大的范围。

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

1 个答案:

答案 0 :(得分:5)

当IF变得复杂时,使用Select Case可能会使事情变得更容易

对于'短'范围;也可以处理“例外”:

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