我想知道是否有人可以帮助我。
我对 ASP 很陌生,我想按如下方式设置当前日期和时间的格式:
yyyy-mm-dd hh:mm:ss
但我能做的就是以下
Response.Write Date
有人可以帮我吗。
默认情况下,Classic ASP 中的日期格式选项受到限制,有一个功能
FormatDateTime()
可以根据服务器区域设置以多种方式格式化您的日期。
尽管有内置的日期时间函数,但可以更好地控制日期格式
Year(date)
- 返回代表年份的整数。通过 Date()
将返回当前年份。
Month(date)
- 返回 1 到 12 之间的整数(含 1 和 12),代表一年中的月份。通过 Date()
将返回当年的当前月份。
MonthName(month[, abbv])
- 返回指示指定月份的字符串。传入 Month(Date())
作为月份将返回当前的月份字符串。 按照@Martha的建议
Day(date)
- 返回 1 到 31 之间的整数(含),代表月份中的第几天。通过 Date()
将返回当月的当前日期。
Hour(time)
- 返回 0 到 23 之间的整数(含),代表一天中的小时。通过 Time()
将返回当前小时。
Minute(time)
- 返回 0 到 59 之间的整数(含),代表一小时中的分钟。通过 Time()
将返回当前分钟。
Second(time)
- 返回 0 到 59 之间的整数(含),代表分钟的秒数。传递 Time()
将返回当前秒。
重要: 当格式化日期/时间值时,始终首先存储日期/时间值。此外,在尝试格式化之前应应用任何所需的计算(
等),否则您将得到意想不到的结果。DateAdd()
函数
Month()
、Day()
、Hour()
、Minute()
和 Second()
都返回整数。幸运的是,有一个简单的解决方法可以让您快速填充这些值 Right("00" & value, 2)
它的作用是将 00
附加到值的前面,然后从右侧获取前两个字符。这可确保返回的所有单位数字值都带有 0
前缀。
Dim dd, mm, yy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue
'Store DateTimeStamp once.
dtsnow = Now()
'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)
'Build the date string in the format yyyy-mm-dd
datevalue = yy & "-" & mm & "-" & dd
'Build the time string in the format hh:mm:ss
timevalue = hh & ":" & nn & ":" & ss
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
dtsvalue = datevalue & " " & timevalue
Call Response.Write(dtsvalue)
注意: 您可以在一次调用中构建日期字符串,但决定将其分解为三个变量以使其更易于阅读。
Date
变量的方法)。