VBS基礎篇 - 常用函數


OptionExplicit
'*********************************Date/Time函數*******************************
'CDate函數把一個合法的日期和事件表達式轉換為Date類型,並返回結果
Dim d1
Dim d2
Dim d3
d1="April 22,2001"
IfIsDate(d1) Then
    MsgBoxCDate(d1)
EndIf

d2=#2/22/01#
IfIsDate(d2) Then
    MsgBoxCDate(d2)
EndIf

d3="3:18:40 AM"
IfIsDate(d3) Then
    MsgBoxCDate(d3)
EndIf

'Date函數返回當前系統的日期
'日期
MsgBoxDate
'日期+時間
MsgBox Now
'時間
MsgBox Time

'DateAdd函數可返回已添加指定時間間隔的日期
MsgBoxDateAdd("yyyy",1,"31-Jan-2003")'加一年
MsgBoxDateAdd("yyyy",1,"31-Jan-2003")'減一年
MsgBoxDateAdd("q",1,"31-Jan-2003")'加一個季度
MsgBoxDateAdd("m",1,"31-Jan-2003")'加一個月
MsgBoxDateAdd("y",1,"31-Jan-2003")'當年的第幾天
MsgBoxDateAdd("d",1,"31-Jan-2003")'增加一天
MsgBoxDateAdd("w",1,"31-Jan-2003")'當周的第幾天
MsgBoxDateAdd("ww",1,"31-Jan-2003")'增加一個周
MsgBoxDateAdd("h",1,"31-Jan-2003")'某日期的第一個小時
MsgBoxDateAdd("n",1,"31-Jan-2003")'某日期的第一分鍾
MsgBoxDateAdd("s",1,"31-Jan-2003")'某日期的第一秒

'DateDiff函數可返回兩個日期之間的時間間隔數
MsgBoxDateDiff("yyyy",Date,"31-Jan-2012")
MsgBoxDateDiff("q",Date,"31-Jan-2012")
MsgBoxDateDiff("m",Date,"31-Jan-2012")
MsgBoxDateDiff("y",Date,"31-Jan-2012")
MsgBoxDateDiff("d",Date,"31-Jan-2012")
MsgBoxDateDiff("w",Date,"31-Jan-2012")
MsgBoxDateDiff("ww",Date,"31-Jan-2012")
MsgBoxDateDiff("h",Date,"31-Jan-2012")
MsgBoxDateDiff("n",Date,"31-Jan-2012")
MsgBoxDateDiff("s",Date,"31-Jan-2012")

'DatePart函數可返回給定日期的指定部分
Dim d
d="2/10/2012 16:25:56"
MsgBoxDatePart("yyyy",d)
MsgBoxDatePart("m",d)
MsgBoxDatePart("q",d)
MsgBoxDatePart("w",d)
MsgBoxDatePart("ww",d)
MsgBoxDatePart("y",d)
MsgBoxDatePart("s",d)
MsgBoxDatePart("h",d)
MsgBoxDatePart("n",d)
MsgBoxDatePart("d",d)

'DateSerial函數可返回指定的年、月、日的子類型Date的Variant
MsgBoxDateSerial(2012,2,30)

'DateValue函數返回一個日期類型
MsgBoxDateValue("31-Jan-2012 2:39:49 AM")

'Day函數可返回介於1到31之間的一個代表月的天數的數字
MsgBoxDay(Date)

'FormatDateTime函數可格式化並返回一個額合法的日期或時間表達式
MsgBoxFormatDateTime(Now,0)
MsgBoxFormatDateTime(Now,1)
MsgBoxFormatDateTime(Now,2)
MsgBoxFormatDateTime(Now,3)
MsgBoxFormatDateTime(Now,4)

'Hour函數可返回介於0到23之間的代表天的小時數的數字
MsgBoxHour(Now)

'IsDate函數可返回一個布爾值,指示經計算的表達式是否可被轉換為日期,如果表達式是日期或可被轉換為日期,則返回True,否則,返回False。
MsgBoxIsDate("15-3-2012")

'Minute函數可返回表示小時的分鍾數的數字
MsgBoxMinute(Now)

'Month函數可返回表示年的月份的數字
MsgBoxMonth(Now)

'Second函數可返回表示分鍾的秒數的數字
MsgBoxSecond(Now)

'Time函數可返回當前的系統時間
MsgBox Time

'Timer函數可返回午夜12時以后已經過去的秒數
MsgBox Timer

'TimeSerial函數可把時、分、秒合並成為時間
MsgBoxTimeSerial(3,45,50)

'TimeValue函數可返回包含時間的日期子類型的變量
MsgBoxTimeValue(Now)

'Weekday函數可返回表示一周的天數的數字,介於1和7之間。
MsgBoxWeekday(Date,0)

'WeekdayName函數可返回一周中指定一天的星期名
MsgBoxWeekdayName(3)
MsgBoxWeekdayName(Weekday(Date,1))

'Year函數可返回表示年份的一個數字
MsgBoxYear(date)

'*********************************Conversion函數*******************************
'Asc函數可把字符串中的第一個字母轉換為對應的ANSI代碼,並返回結果
MsgBoxAsc("A")
MsgBoxAsc("a")

'CBool函數可把表達式轉換為布爾類型
Dim a
a=5
MsgBoxCBool(a)

'CByte函數可把表達式轉換為字節類型
Dim b
b=134.12345
MsgBoxCByte(b)

'CCur函數可把表達式轉換為貨幣類型
Dim c
c=12345.12345656
MsgBox CCur(c)

'CDbl函數可把表達式轉換為雙精度類型
Dim e
e=123.12454656577435235436
MsgBoxCDbl(e)

'Chr函數可把指定的ANSI字符代碼轉換為字符
MsgBoxChr(65)

'CInt函數可把表達式轉換為整數類型
MsgBoxCInt(12.3435)

'CLng函數可把表達式轉換為長整形類型
MsgBoxCLng(12.676)
MsgBoxCLng(12.243)

'CSng函數可把表達式轉換為單精度類型
MsgBoxCSng(122.355465)
MsgBoxCSng(21.23222)

'CStr函數可把表達式轉換為字符串類型
MsgBoxCStr(false)
MsgBoxCStr(Date)
MsgBoxCStr(Empty)
MsgBoxCStr(Error)


'*********************************Format函數*******************************

'FormatCurrency函數可返回作為貨幣值被格式化的表達式,使用系統控制面板中定義的貨幣符號
MsgBoxFormatCurrency(21.23456)

'FormatNumber函數可返回作為數字被格式化的表達式
MsgBoxFormatNumber(12.23456775,3)

'FormatPercent函數可返回作為百分數被格式化的表達式
MsgBoxFormatPercent(3/7,3)

'*********************************Math函數*******************************
'Abs函數可返回指定的數字的絕對值
MsgBoxAbs(-9)

'Atn函數可返回指定數字的正切
MsgBoxAtn(9)

'Cos函數可返回指定數字的余弦
MsgBoxCos(9)

'Exp函數可e的冪次方
MsgBoxExp(2.3)

'Fix函數可返回指定數字的整數部分
MsgBoxFix(-6.325)
MsgBoxInt(-6.325)

'Log函數可返回指定數據的自然對數
MsgBoxLog(10)

'Rnd函數可返回一個隨機數,數字總是小於1但大於或等於0.
MsgBoxRnd

'Sgn函數可返回指定數字的符號的整數
MsgBoxSgn(15)
MsgBoxSgn(-5)
MsgBoxSgn(0)

'Sin函數可返回指定數字的正弦
MsgBoxSin(9)

'Sqr函數可返回一個數的平方根
MsgBoxSqr(9)

'Tan函數可返回指定數字的正切
MsgBoxTan(9)


'*********************************Array函數*******************************
'Array可返回一個包含數組的變量
Dim Arr
Arr=Array(1,2,3,4,5)
MsgBox Arr(0)

'Filter函數可返回一個基於0的數組,此數組包含以特定過濾條件為基礎的字符串數組的子集
Dim Arr1(5),b
Arr1(0)="Saturday"
Arr1(1)="Sunday"
Arr1(2)="Monday"
Arr1(3)="Tuesday"
Arr1(4)="Wednesday"
b=Filter(Arr1,"n",True)
MsgBox b(0)
MsgBox b(1)
MsgBox b(2)

'ISArray函數可返回一個指示指定的變量是否為數組的布爾值。如果變量為數組,則返回True,否則返回False。
Dim a(3)
a(0)=1
a(1)=2
a(2)=3
MsgBoxIsArray(a)

'Join函數可返回一個由某個數組中一系列子字符串組成的字符串
Dim a(5),b
a(0)="Saturday"
a(1)="Sunday"
a(2)="Monday"
a(3)="Wendesday"
a(4)="Tuesday"
b=Filter(a,"n",True)
MsgBoxJoin(b,",")

'LBound函數可返回指示數組維數的最小下標。(始終為0)
'UBound函數可返回指示數組維數的最大下標。
Dim a(5)
MsgBoxLBound(a)
MsgBoxUBound(a)

'Split函數可返回基於0的一維數組,此數組包含指定的子字符串
Dim txt,a
txt="hello/world!"
a=Split(txt,"/")
MsgBox a(0)
MsgBox a(1)

'*********************************String函數*******************************
'InStr函數可返回一個字符串在另一個字符串中首次出現的位置
Dim txt,pos
txt="This is a beautiful day!"
pos=InStr(4,txt,"is",1)
MsgBox pos

'InStrRev函數可返回一個字符串在另一個字符串中首次出現的位置,搜索從字符串的末端開始,但是返回的位置是從字符串的起點開始計數的。
Dim txt,pos
txt="This is a beautiful day!"
pos=InStrRev(txt,"i",-1,1)
MsgBox pos

'LCase函數可把指定的字符串轉換為小寫
'UCase函數可把指定的字符串轉換為大寫
Dim txt,pos1,pos2
txt="This Is A Beautiful Day!"
pos1=LCase(txt)
pos2=UCase(txt)
MsgBox pos1
MsgBox pos2

'Left函數可從字符串的左側返回指定數目的字符
'Right函數可從字符串的右側返回指定數目的字符
Dim txt,pos1,pos2
txt="This is a beautiful day!"
pos1=Left(txt,5)
pos2=Right(txt,6)
MsgBox pos1
MsgBox pos2

'Len函數可返回字符串中字符的數目
Dim txt,pos
txt="This is a beautiful day!"
pos=Len(txt)
MsgBox pos

'LTrim函數可刪除字符串左側的空格
'RTrim函數可刪除字符串右側的空格
'Trim函數可刪除字符串兩端的空格
Dim txt,pos1,pos2,pos3
txt="  This is a beautiful day!  "
pos1=LTrim(txt)
pos2=RTrim(txt)
pos3=Trim(txt)
MsgBox pos1
MsgBox pos2
MsgBox pos3
MsgBoxLen(pos1)
MsgBoxLen(pos2)
MsgBoxLen(pos3)

'Mid函數可從字符串中返回指定數目的字符
Dim txt
txt="This is a beautiful day!"
MsgBoxMid(txt,1,5)
MsgBoxMid(txt,2,5)
MsgBoxMid(txt,1)
MsgBoxMid(txt,5)

'Replace函數可使用一個字符串替換另一個字符串指定的次數
Dim txt
txt="This is a beautiful day!"
MsgBoxReplace(txt,"is","AA",4,2)

'Space函數可返回一個由指定數目的空格組成的字符串
Dim txt
txt=Space(20)
MsgBox txt
MsgBoxLen(txt)

'StrComp函數可比較兩個字符串,並返回表示比較結果的一個值
Dim txt1,txt2
txt1="hello"
txt2="hell"
MsgBoxStrComp(txt1,txt2)

'String函數可返回包含指定長度的重復字符的一個字符串
MsgBoxString(10,"*")

'StrReverse函數可反轉一個字符串
Dim txt
txt="This is a beautiful day!"
MsgBoxStrReverse(txt)

'*********************************其他函數*******************************

'InputBox函數可顯示一個對話框,用戶可在其中輸入文本或點擊一個按鈕。
Dim fname
fname=InputBox("Enter your name:")
MsgBox ("Your name is "&fname)

'IsEmpty函數可返回指定的變量是否被初始化的布爾值。
Dim x,y
y=10
MsgBox IsEmpty(x)
MsgBox IsEmpty(y)

'IsNull函數可返回指定表達式是否無效數據的布爾值
Dim x,y,z
x=Null
y=10
z=""
MsgBox IsNull(x)
MsgBox IsNull(y)
MsgBox IsNull(z)

'IsNumeric函數可返回指示指定的表達式是否可作為數字來計算的布爾值。
Dim x
x=10
MsgBoxIsNumeric(x)
x=Empty
MsgBoxIsNumeric(x)
x="1 h"
MsgBoxIsNumeric(x)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM