rpm posts

Formatting Dates in Classic ASP

During my internship at RPM, we used classic ASP for server side scripting rather than the PHP I'm more used to. Classic ASP is missing functions for formatting dates, like PHP and *NIX shell have, for instance. SQL Server has the CONVERT function, but only has a limited number of output formats, at least for the older SQL Server version we had: Otherwise, it would be more efficient to format the data as it is coming from the database . I built two functions for date formatting based on the PHP and *NIX "date" formats for use on the HSGA site, where I had to format dates a certain way for a project. I don't remember if I used it on other projects, but I think so.

Both functions take two parameters. The first is a date, as would come from a SQL Server "datetime" field. The second is a string that defines the output format. The first function uses a format string like the php date function. The long function is as follows (tabs are double spaces due to the width and length of the content):

Function PHPFormatDateTime(strDateTime, strFormatString)
	Dim intI, strOutput, strTemp, bolEscape
	bolEscape = 0
	
	For intI = 1 to Len(strFormatString)
		If bolEscape = 0 then
			Select Case Mid(strFormatString, intI, 1)
				' day
				Case "d"
					If DatePart("d", strDateTime) < 10 Then
						strOutput = strOutput & "0" & DatePart("d", strDateTime)  
					Else
						strOutput = strOutput & DatePart("d", strDateTime)
					End If
				Case "j"
					strOutput = strOutput & DatePart("d", strDateTime)
				Case "D"
					Select Case DatePart("w", strDateTime)
						Case 1
							strOutput = strOutput &  "Mon"
						Case 2
							strOutput = strOutput &  "Tue"
						Case 3
							strOutput = strOutput &  "Wed"
						Case 4
							strOutput = strOutput &  "Thu"
						Case 5
							strOutput = strOutput &  "Fri"
						Case 6
							strOutput = strOutput &  "Sat"
						Case 7
							strOutput = strOutput &  "Sun"
					End Select
				Case "l"
					Select Case DatePart("w", strDateTime)
						Case 1
							strOutput = strOutput &  "Monday"
						Case 2
							strOutput = strOutput &  "Tuesday"
						Case 3
							strOutput = strOutput &  "Wednesday"
						Case 4
							strOutput = strOutput &  "Thursday"
						Case 5
							strOutput = strOutput &  "Friday"
						Case 6
							strOutput = strOutput &  "Saturday"
						Case 7
							strOutput = strOutput &  "Sunday"
					End Select
				Case "S"
					strTemp = DatePart("d", strDateTime)
					If strTemp < 10 Then
						strTemp = "0" & strTemp
					End If
					Select Case Left(strTemp, 1)
						Case 1
							strOutput = strOutput & "th"
						Case Else
							Select Case Right(strTemp, 1)
								Case 1
									strOutput = strOutput & "st"
								Case 2
									strOutput = strOutput & "cnd"
								Case 3
									strOutput = strOutput & "rd"
								Case Else
									strOutput = strOutput & "th"
							End Select
					End Select
				Case "w"
					strOutput = strOutput & (DatePart("w", strDateTime) - 1)
				Case "Z"
					strOutput = strOutput & DatePart("y", strDateTime)
				' week
				Case "W"
					strOutput = strOutput & DatePart("ww", strDateTime)
				' month
				Case "B"
					Select Case DatePart("m", strDateTime)
						Case 1
							strOutput = strOutput & "January"
						Case 2
							strOutput = strOutput & "February"
						Case 3
							strOutput = strOutput & "March"
						Case 4
							strOutput = strOutput & "April"
						Case 5
							strOutput = strOutput & "May"
						Case 6
							strOutput = strOutput & "June"
						Case 7
							strOutput = strOutput & "July"
						Case 8
							strOutput = strOutput & "August"
						Case 9
							strOutput = strOutput & "September"
						Case 10
							strOutput = strOutput & "October"
						Case 11
							strOutput = strOutput & "November"
						Case 12
							strOutput = strOutput & "December"
					End Select
				Case "m"
					If DatePart("m", strDateTime) < 10 Then
						strOutput = strOutput & "0" & DatePart("m", strDateTime)  
					Else
						strOutput = strOutput & DatePart("m", strDateTime)
					End If
				Case "M"
					Select Case DatePart("m", strDateTime)
						Case 1
							strOutput = strOutput & "Jan"
						Case 2
							strOutput = strOutput & "Feb"
						Case 3
							strOutput = strOutput & "Mar"
						Case 4
							strOutput = strOutput & "Apr"
						Case 5
							strOutput = strOutput & "May"
						Case 6
							strOutput = strOutput & "Jun"
						Case 7
							strOutput = strOutput & "Jul"
						Case 8
							strOutput = strOutput & "Aug"
						Case 9
							strOutput = strOutput & "Sep"
						Case 10
							strOutput = strOutput & "Oct"
						Case 11
							strOutput = strOutput & "Nov"
						Case 12
							strOutput = strOutput & "Dec"
					End Select
				Case "n"
					strOutput = strOutput & DatePart("m", strDateTime)
				' Case "t" ' number of days in given month
				' year
				' Case "L" ' whether it's a leap year
				' Case "o"
				Case "Y"
					strOutput = strOutput & DatePart("yyyy", strDateTime)
				Case "y"
					strOutput = strOutput & Right(DatePart("yyyy", strDateTime), 2)
				' time
				Case "a"
					If DatePart("h", strDateTime) < 12 Then
						strOutput = strOutput & "am"
					Else 
						strOutput = strOutput & "pm"
					End If
				Case "A"
					If DatePart("h", strDateTime) < 12 Then
						strOutput = strOutput & "AM"
					Else 
						strOutput = strOutput & "PM"
					End If
				' Case "B" ' swatch
				Case "g"
					If DatePart("h", strDateTime) = 0 Then
						strOutput = strOutput & 12
					ElseIf DatePart("h", strDateTime) <= 12 Then
						strOutput = strOutput & DatePart("h", strDateTime)
					Else
						strOutput = strOutput & (DatePart("h", strDateTime) - 12)
					End If
				Case "G"
					strOutput = strOutput & DatePart("h", strDateTime)
				Case "h"
					If DatePart("h", strDateTime) = 0 Then
						strTemp = 12
					ElseIf DatePart("h", strDateTime) <= 12 Then
						strTemp = DatePart("h", strDateTime)
					Else
						strTemp = (DatePart("h", strDateTime) - 12)
					End If
					If strTemp < 10 Then
						strOutput = strOutput & 0 & strTemp
					Else
						strOutput = strOutput & strTemp
					End If
				Case "H"
					If DatePart("h", strDateTime) < 10 Then
						strOutput = strOutput & 0 & (DatePart("h", strDateTime))
					Else
						strOutput = strOutput & (DatePart("h", strDateTime))
					End If
				Case "i"
					If DatePart("n", strDateTime) < 10 Then
						strOutput = strOutput & 0 & (DatePart("n", strDateTime))
					Else
						strOutput = strOutput & (DatePart("n", strDateTime))
					End If
				Case "s"
					If DatePart("s", strDateTime) < 10 Then
						strOutput = strOutput & 0 & (DatePart("s", strDateTime))
					Else
						strOutput = strOutput & (DatePart("s", strDateTime))
					End If
				' Case "u" ' microseconds
				' timezone
				' Case "e" ' timezone identifier
				' Case "I" ' daylight savings time boolean
				' Case "O" ' difference to GMT (0x00)
				'Case "P" ' differenct to GMT (0x:00)
				' Case "T" ' timezone abbreviation
				' Case "Z" ' timezone offset in seconds
				' full
				' Case "c" ' IS0 8601
				' Case "r" ' RFC 2822
				' Case "U" ' seconds since Unix epoch
				Case ""
					bolEscape = 1
				Case Else
					strOutput = strOutput & Mid(strFormatString, intI, 1)
			End Select
		Else
			strOutput = strOutput & Mid(strFormatString, intI, 1)
			bolEscape = 0
		End If
	Next
	
	PHPFormatDateTime = strOutput
End Function
Continue reading post "Formatting Dates in Classic ASP"

</toby>