ASP中now()函數可以擷取系統目前時間,這個時間的格式形如"2008-5-19 10:55:26".可是,有時我們更習慣使用"2008年5月19日10時55分26秒"這樣的時間格式.那麼,我們應該如何才能得到這樣的要求呢。
思路一:使用replace()替換函數,具體代碼如下:
<%
function chgtime(str)
if str <> "" then
str = replace(str,"-","年",1,1)'將第一個"-"轉換成"年"
str = replace(str,"-","月",1,1)'將第二個"-"轉換成"月"
str = replace(str," ","日")'將空格" "轉換成"日"
str = replace(str,":","時",1,1)'將冒號":"轉換成"時"
str = replace(str,":","分",1,1)'將冒號":"轉換成"分"
str = str&"秒"'在最後添加"秒"
end if
chgtime=str
end function
Response.Write chgtime("2008-5-19 10:55:26")
%>
運行結果:2008年5月19日10時55分26秒
思路分析:從左至右依次進行替換,具體參照程式碼後面的解釋.
思路二:使用FormatDateTime()函數,具體代碼如下:
<%
function chgtime1(str)
dim str1,str2
if str <> "" then
str1 = FormatDateTime(str,1)'擷取日期部分,得到"2008年5月19日 星期一"
str2 = FormatDateTime(str,3)'擷取擷取時間部分,得到"10:55:26"
end if
chgtime1=str1&" "&str2
end function
Response.Write chgtime1("2008-5-19 10:55:26")
%>
運行結果:2008年5月19日 星期一 10:55:26
思路分析:利用不FormatDateTime()函數的不同參數擷取時間的不同部分再用字串串連符串連.
綜合以上兩種思路,可以得到形如"2008年5月19日 星期一 10時55分26秒"的時間格式.具體代碼如下:
<%
function chgtime2(str)
dim str1,str2
if str <> "" then
str1 = FormatDateTime(str,1)'擷取日期部分
str2 = FormatDateTime(str,3)'擷取擷取時間部分
str2 = replace(str2,":","時",1,1)'將冒號":"轉換成"時"
str2 = replace(str2,":","分",1,1)'將冒號":"轉換成"分"
str2 = str2&"秒"'在最後添加"秒"
end if
chgtime2=str1&" "&str2
end function
Response.Write chgtime2("2008-5-19 10:55:26")
%>
運行結果:2008年5月19日 星期一 10時55分26秒