ASP中時間格式轉換

來源:互聯網
上載者:User

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秒

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.