Reputation: 2774
I have 2 date input fields, Start and End, and I have a String whom I would like to search for a range of dates, the range between Start and End.
For example:
Start: 20/02/2014
End: 25/02/2014
MyText: "In 21/02/2014 something happened"
What I am trying to do is get the dates between Start and End, in this case:
20/02/2014
21/02/2014
22/02/2014
23/02/2014
24/02/2014
25/02/2014
And search in the MyText, if one of the dates are inside it. If yes, I will Response.Write
something.
At first I dunno how to get the array(?) of dates based on the Start and End input fields. And, I can search a string using InStr, but how to search for the array(?)
I just have a single date search, like:
<%
IF InStr(MyText, Now()) >= 1 THEN
%>
Found!
<%
ELSE
%>
Not Found!
<%
END IF
%>
Upvotes: 1
Views: 1345
Reputation: 7500
Ah, you like small code:
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!"
(please note that sd
is always one day ahead of the found date, you need an additional If found then exit do
to circumvent that).
EDIT: After discussion in the comments, I think this is the better approach: Extract the date from the text and see if it is larger or equal than the startdate and smaller or equal than the end date:
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
Upvotes: 1
Reputation: 3111
strStartDate will be your start date
strEndDate will be your end date
First thing we do is set the default value for whether or not we found your string. Next we find the difference between your start and end date in days, we then loop through every day between the start and end date. We use the DateAdd function to determine how far into the loop we are from the start, then we search the MyText string for the date found with the DateAdd function. If we find it, we set the boolDateFound variable to True and exit the loop.
<%
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%>
Upvotes: 1
Reputation: 38745
Use a RegExp to find dd/mm/yyyy dates in your Text, generate a Date (DateSerial) from the parts of each string, compare the found date against the range, store if appropriate.
In Code:
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
output:
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
Upvotes: 2