AI智能
改变未来

ASP中实现字符部位类似.NET里String对象的PadLeft和PadRight函数

论坛里又有人问起日期怎样格式化为:2004-08-09 05:02:20这样的格式:于是写了一个左(右)对齐的函数.这函数等价于.NET里String对象的PadLeft和PadRight函数

Function PadLeft(Value,Length,sChar)Dim strText,IstrText = String(Length,sChar)strText = strText & ValueLeftPad = Right(strText,Length)End Function

改一下最下面则为这样:

Function PadRight(Value,Length,sChar)Dim strText,IstrText = String(Length,sChar)strText = Value & strTextLeftPad = Left(strText,Length)End Function

使用例子:日期显示

AA = year(now) & \”-\” & PadLeft(month(now),2,\”0\”) & \”-\” & PadLeft(Day(now),2,\”0\”) & \” \” & PadLeft(Hour(now),2,\”0\”) & \”:\” & PadLeft(Minute(now),2,\”0\”) & \”:\” & PadLeft(Second(now),2,\”0\”)

万能时间补位代码

Public Function ForMatDate(DateAndTime, Para) \'格式化日期(日期时间,格式)Dim Y, M, D, H, F, SIF Not ISNumeric(Para) Or Not ISDate(DateAndTime) Then Exit FunctionY = CStr(Year(DateAndTime))M = CStr(Month(DateAndTime)):IF Len(M) = 1 Then M = \"0\" & MD = CStr(Day(DateAndTime)):IF Len(D) = 1 Then D = \"0\" & DH = CStr(Hour(DateAndTime)):IF Len(H) = 1 Then H = \"0\" & HF = CStr(Minute(DateAndTime)):IF Len(F) = 1 Then F = \"0\" & FS = CStr(Second(DateAndTime)):IF Len(S) = 1 Then S = \"0\" & SSelect Case ParaCase \"0\"ForMatDate = Y & \"-\" & M & \"-\" & D & \" \" & H & \":\" & F & \":\" & SCase \"1\"ForMatDate = Y & \"-\" & M & \"-\" & D & \" \" & H & \":\" & FCase \"2\"ForMatDate = Y & \"-\" & M & \"-\" & DCase \"3\"ForMatDate = Y & \"/\" & M & \"/\" & DCase \"4\"ForMatDate = Y & \"年\" & M & \"月\" & D & \"日\"Case \"5\"ForMatDate = M & \"-\" & D & \" \" & H & \":\" & FCase \"6\"ForMatDate = M & \"/\" & DCase \"7\"ForMatDate = M & \"月\" & D & \"日\"Case \"8\"ForMatDate = Y & \"年\" & M & \"月\"Case \"9\"ForMatDate = Y & \"-\" & MCase \"10\"ForMatDate = Y & \"/\" & MCase \"11\"ForMatDate = right(Y,2) & \"-\" &M & \"-\" & D & \" \" & H & \":\" & FCase \"12\"ForMatDate = right(Y,2) & \"-\" &M & \"-\" & DCase \"13\"ForMatDate = M & \"-\" & DCase ElseForMatDate = DateAndTimeEnd SelectEnd FunctionResponse.Write ForMatDate(Now,\"2\")

asp一段自动补位的函数

function formatsn(getnum,getbit)dim formatsnnum,formatsnpre,formatsnjformatsnnum = getbit – len(getnum)for formatsnj = 1 to formatsnnumformatsnpre = formatsnpre & \"0\"nextformatsn = formatsnpre & getnumend function

使用方法

formatsn(getnum,getbit)

getnum 计数
getbit 共几位

到此这篇关于ASP中实现字符部位类似.NET里String对象的PadLeft和PadRight函数的文章就介绍到这了,更多相关asp PadLeft和PadRight内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ASP中实现字符部位类似.NET里String对象的PadLeft和PadRight函数