[英]Format current date and time in VBScript
我想知道是否有人可以帮助我。
我是 ASP 的新手,我想按如下方式格式化当前日期和时间:
yyyy-mm-dd hh:mm:ss
但我能做的就是以下
Response.Write Date
有人可以帮帮我吗?
默认情况下,经典 ASP 中的日期格式选项受到限制,有一个函数FormatDateTime()
可以根据服务器区域设置以各种方式格式化您的日期。
尽管有内置的日期时间函数,但为了更好地控制日期格式
Year(date)
-返回代表年份的整数。 通过Date()
将返回当前年份。
Month(date)
-返回 1 到 12(含)之间的整数,表示一年中的月份。 传递Date()
将返回当年的当前月份。
MonthName(month[, abbv])
-返回一个表示指定月份的字符串。 传入Month(Date())
作为月份将返回当前的 Month 字符串。 正如@Martha所建议的
Day(date)
-返回 1 到 31(包括 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
变量的方法) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.