1. 程式人生 > 程式設計 >ASP中格式化時間短日期補0變兩位長日期的方法

ASP中格式化時間短日期補0變兩位長日期的方法

因為短日期不足2位,所以在網頁排版的時候,影響美觀,下面兩個函式可以解決這個問題。

2020-2-7短日期 變 2020-02-07長日期

Function FStime(times)
  Dim years,months,days
   if len(times)=0 then exit function
   years=year(times)
   months=right("0"&month(times),2)
   days=right("0"&day(times),2)
   times=years&"-"&months&"-"&days
   FStime=times
End Function

2020-2-7 23:37:5短日期 變 2020-02-07 23:37:05長日期

Function FLtime(times)
  Dim years,days,hours,minutes,seconds
   if len(times)=0 then exit function
   years=year(times):months=right("0"&month(times),2):hours=right("0"&hour(times),2)
   minutes=right("0"&minute(times),2):seconds=right("0"&second(times),2)
   FLtime=years&"-"&months&"-"&days&" "&hours&":"&minutes&":"&seconds
End Function

Pw_Sys 日期格式轉換函式

<%

Rem Pw_Sys 日期格式轉換函式

function DateTimeFormat(DateTime,Format)
select case Format
case "1"
DateTimeFormat=""&year(DateTime)&"年"&month(DateTime)&"月"&Right("0" & Day(DateTime),2)&"日"
case "2"
DateTimeFormat=""&month(DateTime)&"月"&Right("0" & Day(DateTime),2)&"日"
case "3"
DateTimeFormat=""&year(DateTime)&"-"&month(DateTime)&"-"&Right("0" & Day(DateTime),2)&""
case "4"
DateTimeFormat=""&year(DateTime)&"/"&month(DateTime)&"/"&Right("0" & Day(DateTime),2)&""
case "5"
DateTimeFormat=""&month(DateTime)&"/"&Right("0" & Day(DateTime),2)&""
case "6"
DateTimeFormat=""&year(DateTime)&"年"&month(DateTime)&"月"&Right("0" & Day(DateTime),2)&"日<font color=red> "&FormatDateTime(DateTime,4)&"</font>"
case "7"
  temp="星期日,星期一,星期二,星期三,星期四,星期五,星期六"
  temp=split(temp,",")
  DateTimeFormat=temp(WeekRight("0" & Day(DateTime),2)-1)
case "8"
DateTimeFormat=""&month(DateTime)&"-"&Right("0" & Day(DateTime),2)&""
case "9"
if len(hour(DateTime)) = 1 then
str="0"&hour(DateTime)
else
str=hour(DateTime)
end if
DateTimeFormat=DateTimeFormat(DateTime,1)&" "&str&":"&Minute(DateTime)
case "10"
DateTimeFormat=""&year(DateTime)&"年"&month(DateTime)&"月"
case else
DateTimeFormat=DateTime
end select
end function

%>

程式程式碼(把yyyy-mm-dd格式的日期中的月份和日期轉換成兩位數字的方法)

dim today
today=Date '避免重複呼叫Date,所以賦值給一個變數
today=Year(today) & "-" & Right("0" & Month(today),2) & "-" & Right("0" & Day(today),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中格式化時間短日期補0變兩位長日期的方法的詳細內容,更多關於ASP短日期補0的資料請關注我們其它相關文章!