%
Function DateOutput(dDate, sFormat)
'---------------------------------------------
' DateOutput function for ASP (1.0)
' Created: 2005 by Chris Bloom [ xangelusx AT hotmail DOT com ]
' Last Updated: 2006-07-06 Chris Bloom [ xangelusx AT hotmail DOT com ]
' http://www.csb7.com/
'
' DateOutput function for ASP
' Summary:
' Formats a VBScript date into defined format
'
' Usage:
' To use:
'
' Response.Write DateOutput(Date(),"%l") 'Prints something like: Monday
' Response.Write "
"
' Response.Write DateOutput(Now(),"%l %d%S of %F %Y %h:%i:%s %A") 'Prints something like: Monday 15th of August 2005 03:12:46 PM
' Response.Write "
"
' Response.Write "July 1, 2000 is on a " & DateOutput("July 1, 2000", "%l") 'Prints: July 1, 2000 is on a Saturday
' Response.Write "
"
' Response.Write DateOutput(Date(), "%l the %j%S") 'Prints something like: Wednesday the 15th
' Response.Write "
"
'
' Dim dDate : dDate = "March 10, 2001 5:16:18 pm"
' Response.Write DateOutput(dDate, "%F %j, %Y, %g:%i %a") 'March 10, 2001, 5:16 pm
' Response.Write "
"
' Response.Write DateOutput(dDate, "%m.%d.%y") '03.10.01
' Response.Write "
"
' Response.Write DateOutput(dDate, "%j, %n, %Y") '10, 3, 2001
' Response.Write "
"
' Response.Write DateOutput(dDate, "%Y%m%d") '20010310
' Response.Write "
"
' Response.Write DateOutput(dDate, "%h-%i-%s, %j-%m-%y") '05-16-18, 10-03-01
' Response.Write "
"
' Response.Write DateOutput(dDate, "It is the %j%S day.") 'It is the 10th day.
' Response.Write "
"
' Response.Write DateOutput(dDate, "%D %M %j %G:%i:%s %T %Y") 'Sat Mar 10 17:16:18 %T 2001
' Response.Write "
"
' Response.Write DateOutput(dDate, "%H:%m:%s m is month") '17:03:18 m is month
' Response.Write "
"
' Response.Write DateOutput(dDate, "%H:%i:%s") '17:16:18
' Response.Write "
"
' Response.Write DateOutput(dDate, "%W") '9
' Response.Write "
"
' Response.Write DateOutput(dDate, "%U") '984244578
'
' Thats It!!
'
' Version History:
' Version: 1.0
' Date: Circa 2005
' Author: Chris Bloom
' Version Notes:
' Original version for ASP.
'
' To Do:
' - No improvements currently planned
'
' Notes:
' Please feel free to email me with comments or suggestions:
' xangelusx@hotmail.com
' The code is provided free of charge for non-commercial use so long as you leave all comments intact.
' Please get in touch for commercial licensing information.
'---------------------------------------------
If Not isDate(dDate) Then
DateOutput = ""
Exit Function
Else
dDate = cDate(dDate)
End If
Dim iYear, iYear2, iMonth, iMonth2, iDay, iDay2, iHour, iHour2, iMinute, iMinute2, iSecond, iSecond2
iYear = Year(dDate)
iYear2 = Right(cStr(iYear),2)
iMonth = Month(dDate)
If iMonth < 10 Then iMonth2 = "0" & CStr(iMonth) Else iMonth2 = iMonth
iDay = Day(dDate)
If iDay < 10 Then iDay2 = "0" & CStr(iDay) Else iDay2 = iDay
iHour = Hour(dDate)
If iHour < 10 Then iHour2 = "0" & CStr(iHour) Else iHour2 = iHour
iMinute = Minute(dDate)
If iMinute < 10 Then iMinute2 = "0" & CStr(iMinute) Else iMinute2 = iMinute
iSecond = Second(dDate)
If iSecond < 10 Then iSecond2 = "0" & CStr(iSecond) Else iSecond2 = iSecond
Dim sDate : sDate = sFormat
'ACCEPTED FORMATS
'Day:
'%d Day of the month, 2 digits with leading zeros 01 to 31
sDate = Replace(sDate,"%d",iDay2,1,-1,0)
'%D A textual representation of a day, three letters Sun through Sat
sDate = Replace(sDate,"%D",WeekdayName(WeekDay(dDate,1),TRUE),1,-1,0)
'%j Day of the month without leading zeros 1 to 31
sDate = Replace(sDate,"%j",iDay,1,-1,0)
'%l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
sDate = Replace(sDate,"%l",WeekdayName(WeekDay(dDate),FALSE),1,-1,0)
'%N ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
sDate = Replace(sDate,"%N",WeekDay(dDate,2),1,-1,0)
'%S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
Dim sOrdinal
Select Case iDay
Case 11,12,13:
sOrdinal = "th"
Case Else:
Select Case Right(cStr(iDay),1)
Case "1":
sOrdinal = "st"
Case "2":
sOrdinal = "nd"
Case "3":
sOrdinal = "rd"
Case Else:
sOrdinal = "th"
End Select
End Select
sDate = Replace(sDate,"%S",sOrdinal,1,-1,0)
'%w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
sDate = Replace(sDate,"%w",WeekDay(dDate,1)-1,1,-1,0)
'%z The day of the year (starting from 0) 0 through 365
sDate = Replace(sDate,"%z",DateDiff("d",DateSerial(Year(dDate),1,1),dDate),1,-1,0)
'
'Week
'%W ISO-8601 week number of year, weeks starting on Monday Example: 42 (the 42nd week in the year)
sDate = Replace(sDate,"%W",DateDiff("ww",DateSerial(Year(dDate),1,1),dDate,2,3),1,-1,0)
'
'Month:
'%F A full textual representation of a month, such as January or March January through December
sDate = Replace(sDate,"%F",MonthName(iMonth,False),1,-1,0)
'%m Numeric representation of a month, with leading zeros 01 through 12
sDate = Replace(sDate,"%m",iMonth2,1,-1,0)
'%M A short textual representation of a month, three letters Jan through Dec
sDate = Replace(sDate,"%M",MonthName(iMonth,True),1,-1,0)
'%n Numeric representation of a month, without leading zeros 1 through 12
sDate = Replace(sDate,"%n",iMonth,1,-1,0)
'%t Number of days in the given month 28 through 31
sDate = Replace(sDate,"%t",Day(DateAdd("d",-1,DateSerial(Year(dDate),Month(dDate)+1,1))),1,-1,0)
'
'Year
'%L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
Dim iLeapYear : iLeapYear = 0
If Day(DateAdd("d",-1,DateSerial(Year(dDate),3,1))) = 29 Then iLeapYear = 1
sDate = Replace(sDate,"%L",iLeapYear,1,-1,0)
'%o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. Examples: 1999 or 2003
'sDate = Replace(sDate,"%o",replacewith,1,-1,0) - Not sure how to calculate this
'%Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
sDate = Replace(sDate,"%Y",iYear,1,-1,0)
'%y A two digit representation of a year Examples: 99 or 03
sDate = Replace(sDate,"%y",iYear2,1,-1,0)
'
'Time
'%a Lowercase Ante meridiem and Post meridiem am or pm
Dim sAmPm : sAmPm = "am"
If iHour > 11 Then sAmPm = "pm"
sDate = Replace(sDate,"%a",sAmPm,1,-1,0)
'%A Uppercase Ante meridiem and Post meridiem AM or PM
sDate = Replace(sDate,"%A",UCase(sAmPm),1,-1,0)
'%B Swatch Internet time 000 through 999
'sDate = Replace(sDate,"%B",replacewith,1,-1,0) - Not sure how to calculate this since we can't get timezone from VBScript
'%g 12-hour format of an hour without leading zeros 1 through 12
Dim iHour12 : iHour12 = iHour
If iHour12 > 12 Then
iHour12 = iHour - 12
ElseIf iHour12 = 0 Then
iHour12 = 12
End If
sDate = Replace(sDate,"%g",iHour12,1,-1,0)
'%G 24-hour format of an hour without leading zeros 0 through 23
sDate = Replace(sDate,"%G",iHour,1,-1,0)
'%h 12-hour format of an hour with leading zeros 01 through 12
Dim iHour122 : iHour122 = iHour12
If iHour122 < 9 Then iHour122 = "0" & iHour122
sDate = Replace(sDate,"%h",iHour122,1,-1,0)
'%H 24-hour format of an hour with leading zeros 00 through 23
sDate = Replace(sDate,"%H",iHour2,1,-1,0)
'%i Minutes with leading zeros 00 to 59
sDate = Replace(sDate,"%i",iMinute2,1,-1,0)
'%s Seconds, with leading zeros 00 through 59
sDate = Replace(sDate,"%s",iSecond2,1,-1,0)
'%U
sDate = Replace(sDate,"%U",DateDiff("s","01/01/1970 00:00:00",dDate),1,-1,0)
DateOutput = sDate
End Function
%>