注册 登录
编程论坛 ASP技术论坛

我这个split有错吗?为什么会有这样的结果?

nicechlk 发布于 2008-10-26 18:38, 1991 次点击
function setdate(strdate)
    dim tem,str
    str=split(strdate,"-")  '把日期按-号分割成如:2008,9,4;
    tem=str(0)&"-"
    if len(str(1))=1 then  '如果第2个字符串是1个数量,则:
       str(1)="0"&str(1)   '在前面加0;
    end if
    tem=tem&str(1)&"-"
    if len(str(2))=1 then  '道理同上;
       str(2)="0"&str(2)
    end if
    tem=tem&str(2)
    setdate=tem
end function

说明:日期格式是这样的:2008-9-8 18:30:21
通过自定义函数setdate(current_date),得到这样的格式:2008-09-8 18:30:21,月份可以实现2位09,日子却不能得到2位,这是为何?

[[it] 本帖最后由 nicechlk 于 2008-10-26 18:39 编辑 [/it]]
11 回复
#2
yms1232008-10-26 18:40
楼主这个函数只能使用在日期格式为"2008-9-8"的格式,带时间就会出错。
#3
nicechlk2008-10-26 18:48
哦,是这样!
如果是我的那种日期+时间格式,有办法么?
#4
yms1232008-10-26 18:53
VBS里有一个系统函数FormatDateTime函数可以将时间过滤掉比自己写函数过滤省事
相关MSDN参考
http://msdn.(VS.80).aspx
#5
nicechlk2008-10-26 19:01
嗯,我明白了!
先把时间日期通过函数FormatDateTime转换成字符串,再处理。
谢谢!
#6
hmhz2008-10-26 19:05
程序代码:

<%
function setdate(strdate)
    dim str,tem1,tem2
    str=split(strdate," ")   '把整个时间按空格分割,如:2008-9-4 , 18:30:21
    tem1=split(str(0),"-")   '把日期分割,如:2008 , 9 , 4
    tem2=split(str(1),":")   '把时间分割,如:18 , 30 , 21
    if len(tem1(1))=1 then   '当月长度等于1时
       tem1(1)="0"&tem1(1)   '在前面加0
    end if
    if len(tem1(2))=1 then    '当日长度等于1时
       tem1(2)="0"&tem1(2)    '在前面加0
    end if
    if len(tem2(0))=1 then    '当时长度等于1时
       tem2(0)="0"&tem2(0)    '在前面加0
    end if
    if len(tem2(1))=1 then    '当分长度等于1时
       tem2(1)="0"&tem2(1)    '在前面加0
    end if
    if len(tem2(2))=1 then    '当秒长度等于1时
       tem2(2)="0"&tem2(2)    '在前面加0
    end if
    setdate=tem1(0)&"-"&tem1(1)&"-"&tem1(2)&" "&tem2(0)&":"&tem2(1)&":"&tem2(2)
end function
%>


[[it] 本帖最后由 hmhz 于 2008-10-26 20:52 编辑 [/it]]
#7
nicechlk2008-10-26 20:13
6楼的方法甚好,谢谢!
不过,
tem1=split(str(0),&"-")  '把日期分割,如:2008 , 9 , 4
tem2=split(str(1),&":")  '把时间分割,如:18 , 30 , 21
这里不能有“&”,可能是你手误。
#8
hmhz2008-10-26 20:19
不好意思,手误,纯粹是手误
#9
nicechlk2008-10-26 20:27
呵呵,暇不掩玉!
#10
hmhz2008-10-26 21:23
在复杂的格式化一下
程序代码:

<%
function setdate(str1,str2)
    dim tem,tem1,tem2,tem3,tem4,tem5
    if instr(str1," ")<>0 then
        tem=split(str1," ")   '把整个时间按空格分割,如:2008-9-4 , 8:3:2
        tem1=split(tem(0),"-")   '把日期分割,如:2008 , 9 , 4
        tem2=split(tem(1),":")   '把时间分割,如:8 , 3 , 2
        if len(tem1(1))=1 then   '当月长度等于1时
               tem1(1)="0"&tem1(1)   '在前面加0
        end if
        if len(tem1(2))=1 then    '当日长度等于1时
               tem1(2)="0"&tem1(2)    '在前面加0
        end if
        if len(tem2(0))=1 then    '当时长度等于1时
               tem2(0)="0"&tem2(0)    '在前面加0
        end if
        if len(tem2(1))=1 then    '当分长度等于1时
               tem2(1)="0"&tem2(1)    '在前面加0
        end if
        if len(tem2(2))=1 then    '当秒长度等于1时
               tem2(2)="0"&tem2(2)    '在前面加0
        end if
        tem3=tem1(0)&"-"&tem1(1)&"-"&tem1(2)&" "&tem2(0)&":"&tem2(1)&":"&tem2(2)
    else
        if str2=0 then
            tem3="格式不对"
        end if
        if str2=1 then
            if instr(str1,"-")=0 then
                tem4="格式不对"
            else
                tem1=split(str1,"-")   '把日期分割,如:2008 , 9 , 4
                if len(tem1(1))=1 then   '当月长度等于1时
                       tem1(1)="0"&tem1(1)   '在前面加0
                end if
                if len(tem1(2))=1 then    '当日长度等于1时
                       tem1(2)="0"&tem1(2)    '在前面加0
                end if
                tem4=tem1(0)&"-"&tem1(1)&"-"&tem1(2)
            end if
        end if
        if str2=2 then
            if instr(str1,":")=0 then
                tem5="格式不对"
            else
                tem2=split(str1,":")   '把时间分割,如:8 , 3 , 2
                    if len(tem2(0))=1 then    '当时长度等于1时
                       tem2(0)="0"&tem2(0)    '在前面加0
                end if
                if len(tem2(1))=1 then    '当分长度等于1时
                       tem2(1)="0"&tem2(1)    '在前面加0
                end if
                if len(tem2(2))=1 then    '当秒长度等于1时
                       tem2(2)="0"&tem2(2)    '在前面加0
                end if
                tem5=tem2(0)&":"&tem2(1)&":"&tem2(2)
            end if
        end if
    end if
Select Case str2
Case 0 setdate=tem3
Case 1 setdate=tem4
Case 2 setdate=tem5
End Select
end function
%>

<%=setdate("2008-9-4 8:3:2",0)%>  后面的参数0为整个时间格式化
<%=setdate("2008-9-4",1)%>        后面的参数1为格式化日期
<%=setdate("8:3:2",2)%>           后面的参数2为格式化时间


[[it] 本帖最后由 hmhz 于 2008-10-26 21:59 编辑 [/it]]
#11
hmhz2008-10-26 21:59
再继续复杂完善一下
程序代码:


<%
function setdate(str1,str2)
    dim tem,tem1,tem2,tem3,tem4,tem5
    if instr(str1," ")<>0 then
        if str2=0 then
            tem=split(str1," ")   '把整个时间按空格分割,如:2008-9-4 , 8:3:2
            tem1=split(tem(0),"-")   '把日期分割,如:2008 , 9 , 4
            tem2=split(tem(1),":")   '把时间分割,如:8 , 3 , 2
            if len(tem1(1))=1 then   '当月长度等于1时
                   tem1(1)="0"&tem1(1)   '在前面加0
            end if
            if len(tem1(2))=1 then    '当日长度等于1时
                   tem1(2)="0"&tem1(2)    '在前面加0
            end if
            if len(tem2(0))=1 then    '当时长度等于1时
                   tem2(0)="0"&tem2(0)    '在前面加0
            end if
            if len(tem2(1))=1 then    '当分长度等于1时
                   tem2(1)="0"&tem2(1)    '在前面加0
            end if
            if len(tem2(2))=1 then    '当秒长度等于1时
                   tem2(2)="0"&tem2(2)    '在前面加0
            end if
            tem3=tem1(0)&"-"&tem1(1)&"-"&tem1(2)&" "&tem2(0)&":"&tem2(1)&":"&tem2(2)
            elseif str2=1 then
                str1=split(str1," ")(0)
                if instr(str1,"-")=0 then  '判断 "-" 不存在
                    tem4="格式不对"
                else
                    tem1=split(str1,"-")   '把日期分割,如:2008 , 9 , 4
                    if len(tem1(1))=1 then   '当月长度等于1时
                           tem1(1)="0"&tem1(1)   '在前面加0
                    end if
                    if len(tem1(2))=1 then    '当日长度等于1时
                           tem1(2)="0"&tem1(2)    '在前面加0
                    end if
                    tem4=tem1(0)&"-"&tem1(1)&"-"&tem1(2)
                end if
            elseif str2=2 then
                str1=split(str1," ")(1)
                if instr(str1,":")=0 then  '判断 ":" 不存在
                    tem5="格式不对"
                else
                    tem2=split(str1,":")   '把时间分割,如:8 , 3 , 2
                        if len(tem2(0))=1 then    '当时长度等于1时
                           tem2(0)="0"&tem2(0)    '在前面加0
                    end if
                    if len(tem2(1))=1 then    '当分长度等于1时
                           tem2(1)="0"&tem2(1)    '在前面加0
                    end if
                    if len(tem2(2))=1 then    '当秒长度等于1时
                           tem2(2)="0"&tem2(2)    '在前面加0
                    end if
                    tem5=tem2(0)&":"&tem2(1)&":"&tem2(2)
                end if
            end if
    elseif str2=0 then
            tem3="格式不对"
        elseif str2=1 then
                if instr(str1,"-")=0 then  '判断 "-" 不存在
                    tem4="格式不对"
                else
                    tem1=split(str1,"-")   '把日期分割,如:2008 , 9 , 4
                    if len(tem1(1))=1 then   '当月长度等于1时
                           tem1(1)="0"&tem1(1)   '在前面加0
                    end if
                    if len(tem1(2))=1 then    '当日长度等于1时
                           tem1(2)="0"&tem1(2)    '在前面加0
                    end if
                    tem4=tem1(0)&"-"&tem1(1)&"-"&tem1(2)
                end if
            elseif str2=2 then
                if instr(str1,":")=0 then  '判断 ":" 不存在
                    tem5="格式不对"
                else
                    tem2=split(str1,":")   '把时间分割,如:8 , 3 , 2
                        if len(tem2(0))=1 then    '当时长度等于1时
                           tem2(0)="0"&tem2(0)    '在前面加0
                    end if
                    if len(tem2(1))=1 then    '当分长度等于1时
                           tem2(1)="0"&tem2(1)    '在前面加0
                    end if
                    if len(tem2(2))=1 then    '当秒长度等于1时
                           tem2(2)="0"&tem2(2)    '在前面加0
                    end if
                    tem5=tem2(0)&":"&tem2(1)&":"&tem2(2)
                end if
    end if
Select Case str2
Case 0 setdate=tem3
Case 1 setdate=tem4
Case 2 setdate=tem5
End Select
end function
%>
<%=setdate("2008-9-4 8:3:2",0)%>  后面的参数0为整个时间格式化<br/>
<%=setdate("2008-9-4 8:3:2",1)%>  后面的参数1为只格式化日期<br/>
<%=setdate("2008-9-4 8:3:2",2)%>  后面的参数2为只格式化时间<br/>
<%=setdate("2008-9-4",1)%>        后面的参数1为格式化日期<br/>
<%=setdate("8:3:2",2)%>           后面的参数2为格式化时间<br/>
#12
nicechlk2013-07-03 16:03
皇马果然asp高人。。。。佩服中。。。
1