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

查看:658
本文介绍了在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(含)之间的整数,代表一年中的月份.传递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())作为月份将返回当前的Month字符串. @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之间的一个整数,包括分钟,表示分钟的秒.传递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附加到值的前面,然后从右边开始获取前两个字符.这样可以确保所有单个数字值都返回带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中格式化当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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