We help IT Professionals succeed at work.

Help needed to loop through date array

cdemott33
cdemott33 asked
on
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!
Comment
Watch Question

Commented:
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

Author

Commented:
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.

Commented:
What is happening is that the strMonth variable represents an item in the objMonths.Keys() object.
Commented:
The objMonths object is a Scripting.Dictionary. The Keys are a list of the Month names and the Items for each of those keys are the dates from the original array for that month.
Here is some more info on Scripting.Dictionary objects in VBScript:
http://www.devguru.com/technologies/vbscript/quickref/dictionary.html

 The line that gets the month name is line 6:
strKey = MonthName(Month(CDate(dtDate)))

Open in new window


Month and MonthName are VBScript functions. MonthName takes the number of the month (January = 1 December =12) to get that number I also used Month which takes a date value and returns the month number. Lines 5 - 9 basically check to make sure the value item in the array is actually a date. If it is line 6 sets strKeys to the month name. If not it will set it to "Invalid Dates" so the code doesn't break if the array value isn't a date.

Lines 12 - 16 check to see if the month name is already in the Dictionary object. If not it creates a new Key with the month name and sets the Item value to the date (with formatting set in line 11). If the month already exists then the date is appended to the existing Item values. For instance the first two values in your array are January. So the first time through the loop, January doesn't exist in the Dictionary so it goes into the Else (line 15) and creates the Key and sets the Item. The second time since January already exists it goes to line 13 to append the new date to the list.

After going through the array lists and putting them in the Dictionary, lines 19 - 21 loop through each of the Keys (Month Names) and returns the response for each of the months. objMonths.Keys() just creates an array of all of the month names in the Dictionary so they can be accessed individually in the For Each loop.

Author

Commented:
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!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.