vbscript:日期函数的语法简表

来源:互联网 发布:手机淘宝添加旺旺客服 编辑:程序博客网 时间:2024/06/10 20:36
名称语法功能例句CDateCDate(date)返回date类型的表达式,date 参数是任意有效的日期或字符串表达式。MyDate = "October 19, 1962" MyShortDate = CDate(MyDate)DateDate返回当前系统日期。不带()MyDate = DateDateAddDateAdd(interval, number, date) 返回已添加指定时间间隔的日期。下例将95 年1 月31 日加上一 个月:
NewDate =
  DateAdd(
"m", 1, "31-Jan-95")
DateDiffDateDiff(interval,date1, date2)  返回两个日期之间的时间间隔。xx= DateDiff("d", Now, theDate)DatePartDatePart(interval, date)返回给定日期的指定部分。参考interval参数的可选值。GetQuarter =
    DatePart(
"q", TheDate)
DateSerialDateSerial(year, month, day) 对于指定的年、月、日,返回 Date 子类型的 Variant。MyDate2 =
  DateSerial(
1990-10, 8-2, 1)
DateValueDateValue(date)返回 Date 子类型的变量。date 参数应是字符串。DateValue("September 11, 1963")DayDay(date)返回1 到 31 之间的一个整数,代表某月中的一天。MyDay = Day("October 19, 1962") 'MyDay =19。HourHour(Time)返回0到23之间的一个整数,代表某小时数。Time参数是时间表达式。MyTime = Now
MyHour = Hour(MyTime)
IsDateIsDate(expression)返回Boolean值指明某表达式是否可以转换为日期。expression 参数可以是任意可被识别为日期和时间的日期表达式或字符串表达式。MyDate = "October 19, 1962" YourDate = #10/19/62#
NoDate = "Hello"
MyCheck= IsDate(MyDate) 'True MyCheck= IsDate(YourDate)'True MyCheck= IsDate(NoDate)  'False
MinuteMinute(Time)返回0到59之间的整数,代表分钟。MyVar = Minute(Now)  '=41MonthMonth(date)返回 1到12 之间的一个整数,代表某月份数。MyVar=Minute("October 19, 1962")
'MyVar = 10MonthNameMonthName(month)返回表明指定月份的字符串。month是数值thismanth = MonthName(10)'
’thismanth = "October"
NowNow返回系统的当前日期和时间值。Today = Now
' today = "02-07-04 11:41:14"
SecondSecond(Time) 返回 0到 59之间的一个整数,代表某秒数。MySec = Second(Now)
'
MySec =14
TimeTime返回当前的系统时间。
MyTime = Time
'
MyTime = "11:41:14"
TimerTimer返回午夜 12 时以后已经过去的秒数。 TimeSerialTimeSerial(h,m,s)返回含有指定时h、分m、秒s的时间。MyTime1 = TimeSerial(12-6,-15,0)
' 返回 "5:45:00 AM."
TimeValueTimeValue(time)返回包含时间的 Date子类型的变量。
time 参数通常是代表时间表达式。
MyTime = TimeValue("4:35:17 PM")WeekdayWeekday(date)返回代表一星期中某天的整数0到6。MyDate = #October 19, 1962#
MyWeekDay = Weekday(MyDate)
WeekdayNameWeekdayName(weekday)返回指定某天是星期几的字符串。MyDate= WeekDayName(1)'SundayYearYear(date)返回一个代表某年的整数。MyYear = Year(now'2002FormatDateTimeFormatDateTime(Date[, NamedFormat])返回表达式,此表达式已被格式化为日期或时间。NamedFormat=0,1,2,3,4GetCurrentDate =
    
FormatDateTime(Date, 1)

上表的语法格式中没有括号的表示无参数,date和time分别表示日期类型和时间类型的参数。

部分日期时间型函数用法举例。

关键字  日期时间型函数

代码 

dim today, mydate, mytime
Sub print(x)
x = "<FONT color=red>" &x
x = x &"</FONT>" 
document.write (x)
End sub
document.write "<FONT size=4><p align ='center'>"
document.write "VB部分日期时间型函数用法举例"
document.write "</FONT></P>"
today = date
document.write "Today is "
print today
document.write "<br>"
document.write "变量today 的类型是" &TypeName(today) &"<BR>"
document.write "今天是"
print year(date) &"年" 
print MonthName(Month(date)) 
print day(date) 
print "日<br>"
document.write "用格式化函数FormatDateTime(x,1)转换的日期是"
print formatDateTime(date,1) &"<BR>"
if ( Now = date& time) then 
print "NOW=DATE + TIME<BR>"
else
print "NOW包含DATE 和 TIME<BR>"
end if
document.write "今天是星期几?<BR>"
print "今天是" & WeekdayName(weekday(date)) &"<BR>"
print "今天是星期" & weekday(date)-1 &"<BR>"
today ="march, 21,1994"
Mydate = Cdate(today)
print mydate&"<BR>"
birthday= DateSerial(1994,3,21)
document.write "我的生日是" & birthday&","
num = DateDiff("w", birthday,date)
print "那您已经过了" & num &"周<BR>"
Mydata = DateAdd("d",1,date)
Print "明天是" &FormatDateTime(Mydata,1)
print DateValue("1989-6-4") 
print #1989-6-4#-1 & "<BR>" 
document.write "What time it is Now ? " 
print "Time is " & Time & "<BR>"
document.write "What time it is HOUR ? " 
print "Hour in " & Hour(Time) & "<BR>"
document.write "What time it is Minute?" 
print "Minute is " & Minute(Time) & "<BR>"
document.write "What time it is Second?" 
print "Second is " & Second(Time) & "<BR>"
MyTime = TimeSerial(12-6,15,10)
print Mytime& "<BR>"
document.write "今天是在第"
print DatePart("q",date)
document.write "季度<p>"
document.write"<Font size =4>再见</Font>"

代码注释

  • 给出日期常量的方法有几种。用字符串"1994-3-21","94,03,20","March,21,1994"都可以,当然是在可以接收字符串的函数处使用。必须用日期型的地方可以用“#”号括起来,如#94-3-21#。

  • 月名和星期名在中文版Window98中返回的是“五月”或“星期四”这样的名称,而不是英文名。它取决于系统设定的日期格式。


相关文章

下表给出interval参数的可选值,它适用于dateadd, Datediff, DatePart三个函数。

设置描述yyyyq季度ny一年的日数dw一周的日数wwh小时m分钟s

 

原创粉丝点击