Asp often encounters date format processing problems when making websites. We introduce a useful vbscript function formatdatetime(). For the date stored in the datetime type field of the access database, the display effect is read directly from the database with time. For example, 2009-06 -13 18:00, what should I do if I just want to display the date?
Vbscrip has a function FormatDateTime(). Its description is as follows:
Returns an expression formatted as a date or time.
FormatDateTime(Date[, NamedFormat])
parameter
Date
Required. The date expression to be formatted.
NamedFormat
Optional. A numeric value indicating the date/time format used, if omitted, vbGeneralDate is used.
set up
The NamedFormat parameter can have the following values:
constant | value | describe |
vbGeneralDate | 0 | Display date and/or time. If there is a date part, display that part in short date format. If there is a time part, display that part in long time format. If both exist, all sections are displayed. |
vbLongDate | 1 | Displays the date using the long date format specified in the computer's regional settings. |
vbShortDate | 2 | Displays the date using the short date format specified in the computer's regional settings. |
vbLongTime | 3 | Displays the time using the time format specified in the computer's regional settings. |
vbShortTime | 4 | Displays the time using 24-hour format (hh:mm). |
Strdate=formatdatetime(rs(date),2).
Because there is only one computer region in vbscript and that is the United States, there is only one short format
yyyy-mm-dd.
asp time and date formatted output
1, 2010-10-10 00:00:00
2, 2010-10-10
3, 2010/10/10
4, October 10, 2010 00 hours 00 minutes 00 seconds
5, 10-10 00:00:00
6,10/10
7, October 10
Other transformations can be added by yourself according to the program.
Copy the code code as follows:
'Convert time to time format
Function formatDate(Byval t,Byval ftype)
dim y, m, d, h, mi, s
formatDate=
If IsDate(t)=False Then Exit Function
y=cstr(year(t))
m=cstr(month(t))
If len(m)=1 Then m=0 & m
d=cstr(day(t))
If len(d)=1 Then d=0 & d
h = cstr(hour(t))
If len(h)=1 Then h=0 & h
mi = cstr(minute(t))
If len(mi)=1 Then mi=0 & mi
s = cstr(second(t))
If len(s)=1 Then s=0 & s
select case cint(ftype)
case 1
'yyyy-mm-dd
formatDate=y & - & m & - & d
case 2
'yy-mm-dd
formatDate=right(y,2) & - & m & - & d
case 3
'mm-dd
formatDate=m & - & d
case 4
'yyyy-mm-dd hh:mm:ss
formatDate=y & - & m & - & d & & h & : & mi & : & s
case 5
'hh:mm:ss
formatDate=h & : & mi & : & s
case 6
'Yyyy year mm month dd day
formatDate=y & year & m & month & d & day
case 7
'yyyymmdd
formatDate=y&m&d
case 8
'yyyymmddhhmmss
formatDate=y & m & d & h & mi & s
end select
End Function