ASP中時間格式轉換

2021-04-29 04:57:52 字數 1692 閱讀 3043

asp中now()函式可以獲取系統當前時間,這個時間的格式形如"2008-5-19 10:55:26".可是,有時我們更習慣使用"2023年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")

%>

執行結果:2023年5月19日10時55分26秒

思路分析:從左至右依次進行替換,具體參照**行後面的解釋.

思路二:使用formatdatetime()函式,具體**如下:

<%

function chgtime1(str)

dim str1,str2

if str <> "" then

str1 = formatdatetime(str,1)'獲取日期部分,得到"2023年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")

%>

執行結果:2023年5月19日 星期一 10:55:26

思路分析:利用不formatdatetime()函式的不同引數獲取時間的不同部分再用字串連線符連線.

綜合以上兩種思路,可以得到形如"2023年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")

%>

執行結果:2023年5月19日 星期一 10時55分26秒

ASP中時間格式轉換

asp中now 函式可以獲取系統當前時間,這個時間的格式形如 2008 5 19 10 55 26 可是,有時我們更習慣使用 2008年5月19日10時55分26秒 這樣的時間格式.那麼,我們應該如何才能得到這樣的要求呢?思路一 使用replace 替換函式,具體 如下 function chgti...

時間格式轉換

一 在mysql中完成 這種方式在mysql查詢語句中轉換,優點是不占用php解析器的解析時間,速度快,缺點是只能用在資料庫查詢中,有侷限性。1.unix時間戳轉換為日期用函式 from unixtime 一般形式 select from unixtime 1156219870 2.日期轉換為uni...

時間格式轉換

dateformat函式語法 g 年代標誌符 y 年m 月 d 日h 時 在上午或下午 1 12 h 時 在一天中 0 23 m 分s 秒 s 毫秒 e 星期 d 一年中的第幾天 f 一月中第幾個星期幾 w 一年中第幾個星期 w 一月中第幾個星期 a 上午 下午 標記符 k 時 在一天中 1 24 ...