欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 17:13:05
论坛里又有人问起日期怎样格式化为:2004-08-09 05:02:20这样的格式:于是写了一个左(右)对齐的函数.这函数等价于.net里string对象的padleft和padright函数func...

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

function padleft(value,length,schar)
  dim strtext,i
  strtext = string(length,schar)
  strtext = strtext & value
  leftpad = right(strtext,length)
end function

改一下最下面则为这样:

function padright(value,length,schar)
  dim strtext,i
  strtext = string(length,schar)
  strtext = value & strtext
  leftpad = 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, s 
if not isnumeric(para) or not isdate(dateandtime) then exit function 
y = cstr(year(dateandtime)) 
m = cstr(month(dateandtime)):if len(m) = 1 then m = "0" & m 
d = cstr(day(dateandtime)):if len(d) = 1 then d = "0" & d 
h = cstr(hour(dateandtime)):if len(h) = 1 then h = "0" & h 
f = cstr(minute(dateandtime)):if len(f) = 1 then f = "0" & f 
s = cstr(second(dateandtime)):if len(s) = 1 then s = "0" & s 
select case para 
  case "0" 
    formatdate = y & "-" & m & "-" & d & " " & h & ":" & f & ":" & s 
  case "1" 
    formatdate = y & "-" & m & "-" & d & " " & h & ":" & f 
  case "2" 
    formatdate = y & "-" & m & "-" & d 
  case "3" 
    formatdate = y & "/" & m & "/" & d 
  case "4" 
    formatdate = y & "年" & m & "月" & d & "日" 
  case "5" 
    formatdate = m & "-" & d & " " & h & ":" & f 
  case "6" 
    formatdate = m & "/" & d 
  case "7" 
    formatdate = m & "月" & d & "日" 
  case "8" 
    formatdate = y & "年" & m & "月" 
  case "9" 
    formatdate = y & "-" & m 
  case "10" 
    formatdate = y & "/" & m 
  case "11" 
    formatdate = right(y,2) & "-" &m & "-" & d & " " & h & ":" & f 
  case "12" 
    formatdate = right(y,2) & "-" &m & "-" & d 
  case "13" 
    formatdate = m & "-" & d 
  case else 
    formatdate = dateandtime 
end select 
end function 
response.write formatdate(now,"2")

asp一段自动补位的函数

function formatsn(getnum,getbit)
dim formatsnnum,formatsnpre,formatsnj
formatsnnum = getbit – len(getnum)
for formatsnj = 1 to formatsnnum
formatsnpre = formatsnpre & "0"
next
formatsn = formatsnpre & getnum
end function

使用方法

formatsn(getnum,getbit)

getnum 计数
getbit 共几位

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