在 VBScript 中格式化当前日期和时间 [英] Format current date and time in VBScript

查看:38
本文介绍了在 VBScript 中格式化当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.

I was wondering if someone could help me.

我是 ASP 的新手,我想按如下方式格式化当前日期和时间:

I'm very new at ASP I want to format the current date and time as follows:

yyyy-mm-dd hh:mm:ss

但我能做的就是以下

Response.Write Date

有人可以帮我吗.

推荐答案

默认情况下,Classic ASP 中的日期格式化选项是有限的,有一个函数 FormatDateTime() 可以通过多种方式格式化您的日期基于服务器区域设置.

Date formatting options are limited in Classic ASP by default, there is a function FormatDateTime() which can format your date is various ways based on the servers regional settings.

为了更好地控制日期格式,虽然有内置的日期时间函数

For more control over date formatting though there are built in date time functions

  • Year(date) - 返回代表年份的整数.传递 Date() 将返回当前年份.

  • Year(date) - Returns a whole number representing the year. Passing Date() will give back the current year.

Month(date) - 返回 1 到 12(包括 1 和 12)之间的整数,表示一年中的月份.传递 Date() 将返回当年的当前月份.

Month(date) - Returns a whole number between 1 and 12, inclusive, representing the month of the year. Passing Date() will return the current month of the year.

MonthName(month[, abbv]) - 返回表示指定月份的字符串.传入 Month(Date()) 作为月份将返回当前月份字符串.正如 @Martha

MonthName(month[, abbv]) - Returns a string indicating the specified month. Passing in Month(Date()) as the month will give back the current Month string. As suggested by @Martha

Day(date) - 返回 1 到 31(包括 1 和 31)之间的整数,表示一个月中的某天.传递 Date() 将返回当月的当前日期.

Day(date) - Returns a whole number between 1 and 31, inclusive, representing the day of the month. Passing Date() will return the current day of the month.

Hour(time) - 返回 0 到 23 之间的整数,包括 0 和 23,代表一天中的小时.传递 Time() 将返回当前小时.

Hour(time) - Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Passing Time() will return the current hour.

Minute(time) - 返回 0 到 59 之间的整数,包括 0 和 59,代表一小时的分钟.传递 Time() 将返回当前分钟.

Minute(time) - Returns a whole number between 0 and 59, inclusive, representing the minute of the hour. Passing Time() will return the current minute.

Second(time) - 返回 0 到 59 之间的整数,包括 0 和 59,代表一分钟的秒.传递 Time() 将返回当前秒.

Second(time) - Returns a whole number between 0 and 59, inclusive, representing the second of the minute. Passing Time() will return the current second.

重要提示:格式化日期/时间值时,始终先存储日期/时间值.此外,任何需要的计算(DateAdd() 等) 都应该在尝试格式化之前应用,否则你会得到意想不到的结果.

IMPORTANT: When formatting date / time values, always store the date / time value first. Also, any needed calculations (DateAdd() etc.) should be applied before attempting to format or you will get unexpected results.

函数 Month()Day()Hour()Minute()Second() 都返回整数.幸运的是,有一个简单的解决方法可以让您快速填充这些值 Right("00" & value, 2) 它的作用是将 00 附加到value 然后从右边取前两个字符.这可确保所有单个数字值返回前缀为 0.

The functions Month(), Day(), Hour(), Minute() and Second() all return whole numbers. Luckily there is an easy workaround that lets you pad these values quickly Right("00" & value, 2) what it does is append 00 to the front of the value then from the right take the first two characters. This ensures that all single digit values return prefixed with a 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)

注意: 您可以在一次调用中构建日期字符串,但决定将其分解为三个变量以使其更易于阅读.

  • How Can I Format Date
  • Example of Parsing a Date String (Answers provide approaches to taking a date string format and parsing it to a valid Date variable).
  • Format the date of the previous day format yyyymmdd with VBScript (Example of why storing date / time before performing formatting is important)
  • VBScript ISO8601 (Example of functions to construct an ISO 8601 compliant date string)

这篇关于在 VBScript 中格式化当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆