|
This function was created to compensate for the poor date and time formatting capabilities available to the ASP (VBScript) developer. FormatDateTime() just doesn't cut it! This function is analgous to the Format() function in the Visual Basic world. The function takes two parameters: - A valid date object
- A formatting string such as
'mm-dd-yyyy'
The first parameter can be a Variant Date object or an expression that evaluated to Variant Date object. The second parameter is a Variant String composed of the following date and time tokens: yy - year value, 2-digits yyyy - year value, 4-digits m - month value (1 to 12), not zero-padded mm - month value (1 to 12), zero-padded d - day value (1 to 31), not zero-padded dd - day value (1 to 31), zero-padded
h - hours value, not zero-padded hh - hours value, zero-padded m or mm - minutes value, always zero-padded s or ss - seconds value, always zero-padded
- forward slash(/)
- hyphen (-)
If you to use date and time formatting, then separate the date from the time with a space. Note: fmrDateTime requires a small utility function called ZeroPads to -- you guessed it -- zero-pad values. It is included below. <%
Dim dToday, dXmas2001
Response.Write "Today's date: " & fmtDateTime(Now(), "yyyy-mm-dd")
Response.Write "Yesterday's date: " & fmtDateTime(DateAdd("d", -1, Now()), "yyyy-mm-dd")
dXmas2001 = #12/25/2001 12:45:50#
Response.Write "Christmas 2001: : " & fmtDateTime(dXmas2001, "m/dd/yyyy hh:mm")
%>
<%
Function fmtDateTime(byval d, byval pat)
Dim Tokens, token, delim, i, date_part, time_part, DateTokens, TimeTokens
If IsNull(d) Then
fmtDateTime = ""
Exit Function
End If
If TypeName(d) <> "Date" Or Not IsDate(d) Then
fmtDateTime = "Invalid date parameter."
Exit Function
End If
If InStr(pat, " ") > 0 Then
Tokens = Split(pat, " ") ' should be 2 tokens --> (0)date (1)time
Else
Tokens = Split(pat, "") ' date OR time formatting
End If
bIsDate = False : bIsTime = False
For Each token In Tokens
If InStr(token, "-") > 0 Or InStr(token, "/") > 0 Then
If InStr(token, "-") Then
delim = "-"
ElseIf InStr(token, "/") Then
delim = "/"
End If
DateTokens = Split(token, delim)
For i = 0 To UBound(DateTokens)
Select Case CStr(DateTokens(i))
Case "yy"
DateTokens(i) = Right(CStr(DatePart("yyyy", d)), 2)
Case "yyyy"
DateTokens(i) = CStr(DatePart("yyyy", d))
Case "m"
DateTokens(i) = CStr(DatePart("m", d))
Case "mm"
DateTokens(i) = ZeroPad(CStr(DatePart("m", d)),2)
Case "d"
DateTokens(i) = CStr(DatePart("d", d))
Case "dd"
DateTokens(i) = ZeroPad(CStr(DatePart("d", d)),2)
Case Else
fmtDateTime = "Invalid date format : " & token
Exit Function
End Select
Next
date_part = Join(DateTokens, delim)
End If
If InStr(token, ":") > 0 Then
TimeTokens = Split(token, ":")
For i = 0 To UBound(TimeTokens)
Select Case CStr(TimeTokens(i))
Case "h"
TimeTokens(i) = Right(CStr(DatePart("h", d)), 2)
Case "hh"
TimeTokens(i) = ZeroPad(CStr(DatePart("h", d)),2)
Case "m", "mm" ' always zero-pad minutes
TimeTokens(i) = ZeroPad(CStr(DatePart("n", d)),2)
Case "s", "ss" ' always zero-pad seconds
TimeTokens(i) = ZeroPad(CStr(DatePart("s", d)),2)
Case Else
fmtDateTime = "Invalid time format : " & token
Exit Function
End Select
Next
time_part = Join(TimeTokens, ":")
End If
Next
fmtDateTime = Trim(date_part & " " & time_part)
End Function
Function ZeroPad(byval str, byval iSize)
ZeroPad = String((iSize - Len(str)), "0") & Trim(str)
End Function
%>
|