Link to home
Start Free TrialLog in
Avatar of cdemott33
cdemott33Flag for United States of America

asked on

Help needed to loop through date array

I have an array that I need to loop though and display something like this on my webpage:

January
- 1/2/2012
- 1/14/2012

February
- 2/19/2012

April
- 4/20/2012

July
- 7/4/2012

The array looks like this

0 = 1/2/2012
1 = 1/14/2012
2 = 2/19/2012
3 = 4/20/2012
4 = 7/4/2012

It's a classic ASP/VBScript page.  I need to print the full month's name followed by the dates that fall within that month.  Can someone provide me with a code snippet that will give me the output I'm looking for.  I'm lost on how I can achieve this.  Thanks!
Avatar of DSmithVz
DSmithVz

Do you know if the months  the dates are in in the array are in order, if not do they need to be sorted? The below code will provide the output based on what you stated in the question, but if the month need to be sorted there can be some modifciations made to this to make that work.

aryValues = Array("1/2/2012","1/14/2012","2/19/2012","4/20/2012","7/4/2012")
Set objMonths = CreateObject("Scripting.Dictionary")

For Each dtDate In aryValues
	If IsDate(dtDate) Then
		strKey = MonthName(Month(CDate(dtDate)))
	Else
		strKey = "Invalid Dates"
	End If

	strValue = "<br /> - " & CStr(dtDate)
	If objMonths.Exists(strKey) Then
		objMonths.Item(strKey) = objMonths.Item(strKey) & strValue
	Else	
		objMonths.Item(strKey) = strValue
	End If
Next

For Each strMonth In objMonths.Keys()
	Response.Write (strMonth & objMonths.Item(strMonth) & "<br /><br />")
Next

Set objMonths = Nothing

Open in new window

Avatar of cdemott33

ASKER

Works perfect!  Thank you.  Question though... just so I understand.  In this block of code...

For Each strMonth In objMonths.Keys()
      Response.Write (strMonth & objMonths.Item(strMonth) & "<br /><br />")
Next

It's the first time the variable strMonth is declared.  Does the objMonths.Keys() assign this variable the name of the month?  Sorry, I'm just not sure how this works and I want to learn.
What is happening is that the strMonth variable represents an item in the objMonths.Keys() object.
ASKER CERTIFIED SOLUTION
Avatar of DSmithVz
DSmithVz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much for the detail explanation.  It really helped me understand your script.  It's people like you that keep me a member of Experts Exchange.  Great Job!