• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

Parse html code for dates with vbscript

I'm looking to extract dates from an HTML page using vbscript. Here's an example of the raw data:

<TABLE class=contract_table cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD class=contract_header width="36%">Description</TD>
<TD class=contract_header width="16%">Provider</TD>
<TD class=contract_header width="16%">Start Date</TD>
<TD class=contract_header width="16%">End Date</TD>
<TD class=contract_header style="BORDER-RIGHT-WIDTH: 0px" width="16%">Days Left</TD></TR>
<TR>
<TD class=contract_oddrow>UNY</TD>
<TD class=contract_oddrow>5/12/2008</TD>
<TD class=contract_oddrow>5/12/2010</TD>
<TD class=contract_oddrow style="BORDER-RIGHT-WIDTH: 0px">553</TD></TR>

I'm looking for code that will find and return the most recent date found in the data. The date will either appear as MM/DD/YYYY or M/DD/YYYY.

Thanks.
0
mrpez1
Asked:
mrpez1
1 Solution
 
hieloCommented:
the attached code will give you 5/12/2010. If you are interested in 5/12/2008 then change the else if to Greater than instead:
ElseIf CDate(lastDate) > CDate(Trim(cell.innerHTML)) Then
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/vbscript">
Function recentDate
	Dim t
	Set td = document.getElementsByTagName("td")
	Dim lastDate
	lastDate = False
	For Each cell in td
		If IsDate(cell.innerHTML) Then
			If VarType(lastDate) = vbBoolean Then
				lastDate = Trim(cell.innerHTML)
			ElseIf CDate(lastDate) < CDate(Trim(cell.innerHTML)) Then
				lastDate = Trim(cell.innerHTML)
			End If
		End If
	Next
	recentDate = lastDate
End Function 
Sub init
	msgbox recentDate()
End Sub
</script>
</head>
<body onload="call init()">
<TABLE class=contract_table cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD class=contract_header width="36%">Description</TD>
<TD class=contract_header width="16%">Provider</TD>
<TD class=contract_header width="16%">Start Date</TD>
<TD class=contract_header width="16%">End Date</TD>
<TD class=contract_header style="BORDER-RIGHT-WIDTH: 0px" width="16%">Days Left</TD></TR>
<TR>
<TD class=contract_oddrow>UNY</TD>
<TD class=contract_oddrow>5/12/2008</TD>
<TD class=contract_oddrow>5/12/2010</TD>
<TD class=contract_oddrow style="BORDER-RIGHT-WIDTH: 0px">553</TD></TR>
</TBODY>
</TABLE>
</body>
</html>

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now