ASP
Here you can find some FREE usefull Classic ASP / VBScript code snippets, class modules, etc.
Function Format(Variant,String)String
This is a very usefull function that you can find on VB but not on ASP (VBScript)
Example
I've implemented it to fill most of my formatting needs (dates and numbers), but it's not a full implementation of VB's Format function. Please note that you must hard-code the constant values for digit separators, month names, etc., according to your country or needs. Usage Format (Value, Mask) Value - Numeric or Date value Mask - Formatting mask Numeric value mask format: # - Any number ; 0 - Number or zero Date value mask format: yyyy - 4 digit year ; yy - 2 digit year ; MM - Month number ; MMM - Month name ; dd - Day number ; DD - Weekday ; HH - Hour ; mm - Minute ; ss - Second <% response.write Format(12345,"0,00") ' will output 12345,00 response.write Format(12345,"#.0,00") ' will output 12.345,00 response.write Format(12.345,"0,00") ' will output 12,34 (note that unlike VB's Format function the this function will not round up decimals) response.write Format(cdate("2011-03-01 12:30:45"),"dd-MM-yyyy") ' will output 01-03-2011 response.write Format(cdate("2011-03-01 12:30:45"),"MMM yyyy, day dd") ' will output March 2011, day 01 %> Source Code <%
Public Function Format(vl, msk) Dim a, tmp, i, ires, dres, svl, dsep, gsep, n, m, gflag, sflag ' Set this values according to your preferences Const decimalSeparatorDigit = "," Const groupingDigit = "." Const weekDays = "Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;Sunday" Const monthNames = "January;February;March;April;May;June;July;August;September; October;November;December" If IsDate(vl) Then tmp = msk If InStr(tmp, "yy") > 0 Then tmp = Replace(tmp, "yy", "YY") If InStr(tmp, "hh") > 0 Then tmp = Replace(tmp, "hh", "HH") If InStr(tmp, "YYYY") > 0 Then tmp = Replace(tmp, "YYYY", Format(Year(vl), "0000")) If InStr(tmp, "YY") > 0 Then tmp = Replace(tmp, "YY", Format(Right(Year(vl), 2), "00")) If InStr(tmp, "MMM") > 0 Then tmp = Replace(tmp, "MMM", Split(monthNames, ";")(Month(vl))) If InStr(tmp, "MM") > 0 Then tmp = Replace(tmp, "MM", Format(Month(vl), "00")) If InStr(tmp, "DD") > 0 Then tmp = Replace(tmp, "DD", Split(weekDays, ";")(Weekday(vl, 2) - 1)) If InStr(tmp, "dd") > 0 Then tmp = Replace(tmp, "dd", Format(Day(vl), "00")) If InStr(tmp, "HH") > 0 Then tmp = Replace(tmp, "HH", Format(Hour(vl), "00")) If InStr(tmp, "mm") > 0 Then tmp = Replace(tmp, "mm", Format(Minute(vl), "00")) If InStr(tmp, "ss") > 0 Then tmp = Replace(tmp, "ss", Format(Second(vl), "00")) Format = tmp: Exit Function End If If IsNumeric(vl) Then dsep = Mid(CStr(0.5), 2, 1): If dsep = "." Then gsep = "," Else gsep = "." sflag = (dsep <> decimalSeparatorDigit Or gsep <> groupingDigit) svl = CStr(CCur(vl)): If InStr(svl, dsep) = 0 Then svl = svl & dsep & "0" n = Split(svl, dsep) If sflag Then If InStr(msk, decimalSeparatorDigit) > 0 Then msk = Replace(msk, decimalSeparatorDigit, Chr(0)) If InStr(msk, groupingDigit) > 0 Then msk = Replace(msk, groupingDigit, Chr(1)) If InStr(msk, Chr(0)) > 0 Then msk = Replace(msk, Chr(0), dsep) If InStr(msk, Chr(1)) > 0 Then msk = Replace(msk, Chr(1), gsep) End If If InStr(msk, dsep) = 0 Then msk = msk & dsep m = Split(msk, dsep) If InStr(m(0), gsep) > 0 Then gflag = True: m(0) = Replace(m(0), gsep, "") Else gflag = False End If For i = Len(m(0)) To 1 Step -1 Select Case Mid(m(0), i, 1) Case "#" If n(0) > "" Then If CLng(n(0)) = 0 Then a = "" Else a = CLng(n(0)): n(0) = "" End If Else a="" End If Case "0" a = Right(n(0), 1): If n(0) > "" Then n(0) = Left(n(0), Len(n(0)) - 1) If a = "" Then a = "0" Case Else a = Mid(m(0), i, 1) End Select ires = a & ires Next If n(0) > "" Then ires = n(0) & ires If gflag Then i = 0 Do While ires > "" i = i + 1: tmp = Right(ires, 1) & tmp: ires = Left(ires, Len(ires) - 1) If i = 3 And IsNumeric(Right(ires, 1)) Then tmp = gsep & tmp: i = 0 Loop ires = tmp End If For i = 1 To Len(m(1)) Select Case Mid(m(1), i, 1) Case "#" If CLng(n(1)) = 0 Then a = "" Else a = CLng(n(1)) Case "0" a = Left(n(1), 1): If n(1) > "" Then n(1) = Mid(n(1), 2) If a = "" Then a = "0" Case Else a = Mid(m(1), i, 1) End Select dres = dres & a Next If dres > "" Then ires = ires & dsep & dres If sflag Then If InStr(ires, dsep) > 0 Then ires = Replace(ires, dsep, Chr(0)) If InStr(ires, gsep) > 0 Then ires = Replace(ires, gsep, Chr(1)) If InStr(ires, Chr(0)) > 0 Then ires = Replace(ires, Chr(0), decimalSeparatorDigit) If InStr(ires, Chr(1)) > 0 Then ires = Replace(ires, Chr(1), groupingDigit) End If Format = ires End If End Function %> |