我有2个日期输入字段,开始和结束,我有一个字符串,我想搜索一系列日期,范围开始和结束。
例如:
Start: 20/02/2014
End: 25/02/2014
MyText: "In 21/02/2014 something happened"
我要做的是获取开始和结束之间的日期,在这种情况下:
20/02/2014
21/02/2014
22/02/2014
23/02/2014
24/02/2014
25/02/2014
并在 MyText 中搜索,如果其中一个日期在其中。如果是的话,我会Response.Write
。
首先,我不知道如何根据开始和结束输入字段获取日期数组(?)。而且,我可以使用 InStr 搜索字符串,但是如何搜索数组(?)
我只有一个日期搜索,例如:
<%
IF InStr(MyText, Now()) >= 1 THEN
%>
Found!
<%
ELSE
%>
Not Found!
<%
END IF
%>
答案 0 :(得分:2)
使用RegExp在文本中查找dd / mm / yyyy日期,从每个字符串的部分生成日期(DateSerial),将找到的日期与范围进行比较,存储(如果适用)。
在代码中:
Option Explicit
Dim greDate : Set greDate = New RegExp
greDate.Global = True
greDate.Pattern = "(\d{2})/(\d{2})/(\d{4})" ' dd/mm/yyyy
Dim aTests : aTests = Array( _
"In 21/02/2014 something happened" _
, "" _
, "pi 19/02/2014 pa 26/02/2014 po" _
, "In 21/02/2013 something happened" _
, "pi 19/02/2014 pu 20/02/2014 pa 21/02/2014 22/02/2014 23/02/2014 24/02/2014 25/02/2014 26/02/2014 po" _
)
Dim sTest
Dim dtFrom : dtFrom = #2/20/2014#
Dim dtTo : dtTo = #2/25/2014#
For Each sTest In aTests
WScript.Echo "-----", qq(sTest)
Dim aDates : aDates = getDatesFrom(sTest, dtFrom, dtTo)
If -1 = UBound(aDates) Then
WScript.Echo " no interesting dates found."
Else
WScript.Echo " found (m/d/yyyy!)", Join(aDates, ", ")
End If
Next
Function getDatesFrom(sText, dtFrom, dtTo)
ReDim aTmp(-1)
Dim oMTS : Set oMTS = greDate.Execute(sText)
Dim oMT, dtFound
For Each oMT In oMTS
' dd/mm/yyyy
dtFound = DateSerial(CInt(oMT.SubMatches(2)), cInt(oMT.SubMatches(1)), CInt(oMT.SubMatches(0)))
If dtFound >= dtFrom And dtFound <= dtTo Then
ReDim Preserve aTmp(Ubound(aTmp) + 1)
aTmp(Ubound(aTmp)) = dtFound
End If
Next
getDatesFrom = aTmp
End Function
Function qq(s) : qq = """" & s & """" : End Function
输出:
cscript 21994835.vbs
----- "In 21/02/2014 something happened"
found (m/d/yyyy!) 2/21/2014
----- ""
no interesting dates found.
----- "pi 19/02/2014 pa 26/02/2014 po"
no interesting dates found.
----- "In 21/02/2013 something happened"
no interesting dates found.
----- "pi 19/02/2014 pu 20/02/2014 pa 21/02/2014 22/02/2014 23/02/2014 24/02/2014 25/02/2014 26/02/2014 po"
found (m/d/yyyy!) 2/20/2014, 2/21/2014, 2/22/2014, 2/23/2014, 2/24/2014, 2/25/2014
答案 1 :(得分:1)
strStartDate将是您的开始日期
strEndDate将是您的结束日期
我们要做的第一件事是设置我们是否找到你的字符串的默认值。接下来,我们找到您的开始日期和结束日期之间的差异,然后我们循环开始和结束日期之间的每一天。我们使用DateAdd函数来确定从开始到循环的距离,然后我们在MyText字符串中搜索使用DateAdd函数找到的日期。如果我们找到它,我们将boolDateFound变量设置为True并退出循环。
<%
boolDateFound = False
For intDateDiff = 0 to DateDiff("d",strStartDate,strEndDate)
arrDateParts = Split(DateAdd("d",intDateDiff,strStartDate),"/")
If arrDateParts(1) <= 9 Then
arrDateParts(1) = "0" & arrDateParts(1)
End If
strCheckDate = arrDateParts(0) & "/" & arrDateParts(1) & "/" & arrDateParts(2)
Response.Write "Date: " & strCheckDate
If InStr(MyText, strCheckDate) > 0 Then
boolDateFound = True
Exit For
End If
Next
If boolDateFound Then%>
Found!
<%Else%>
Not Found
<%End If%>
答案 2 :(得分:1)
啊,你喜欢小代码:
sd = cDate("20/02/2014")
ed = cDate("25/02/2014")
testString = "In 21/02/2014 something happened"
do
found = (instr(testString, CreateObject("System.Text.Stringbuilder").AppendFormat("{0:dd}/{0:MM}/{0:yyyy}", sd).ToString()) > 0)
sd = cDate(sd + 1)
loop until sd > ed or found
if found then wscript.echo "I found the date!"
(请注意,sd
始终比找到的日期提前一天,您需要额外If found then exit do
来规避这一点。
编辑:在评论中讨论后,我认为这是更好的方法:从文本中提取日期,看它是否大于或等于开始日期,小于或等于结束日期:
sd = cDate("20/02/2014")
ed = cDate("25/02/2014")
testString = "In 21/02/2014 something happened"
set re = new regexp
re.pattern = "\d{1,2}/\d{1,2}/\d{4}"
for each match in re.execute(testString)
if cDate(match) >= sd and cDate(match) <= ed then wscript.echo "Found date " & match
next