VBS显示当前标准时间
VBS显示当前标准时间,例如:执行下面的代码则显示:2013-05-1119:10:11
OptionExplicit DimblnDate,blnTime DimdtmDate DimintDay,intFormat,intHour,intMin,intMonth,intSec,intUTC,intValid,intYear DimstrISO WithWScript.Arguments 'Checkcommandlinearguments If.Unnamed.Count=0ThendtmDate=Now If.Unnamed.Count>0ThendtmDate=.Unnamed(0) If.Unnamed.Count>1ThendtmDate=dtmDate&""&.Unnamed(1) If.Unnamed.Count>2ThendtmDate=dtmDate&""&.Unnamed(2) If.Unnamed.Count>3ThenSyntax OnErrorResumeNext dtmDate=CDate(dtmDate) IfErrThen OnErrorGoto0 Syntax EndIf OnErrorGoto0 IfNotIsDate(dtmDate)ThenSyntax intValid=0 blnDate=True blnTime=True If.Named.Exists("D")Then blnDate=True blnTime=False intValid=intValid+1 EndIf If.Named.Exists("T")Then blnDate=False blnTime=True intValid=intValid+1 EndIf IfintValid<>.Named.CountThenSyntax IfintValid>1ThenSyntax EndWith 'Formattheoutputstring intYear=DatePartLZ("yyyy",dtmDate) intMonth=DatePartLZ("m",dtmDate) intDay=DatePartLZ("d",dtmDate) intHour=DatePartLZ("h",dtmDate) intMin=DatePartLZ("n",dtmDate) intSec=DatePartLZ("s",dtmDate) IfblnDateThenstrISO=intYear&"-"&intMonth&"-"&intDay IfblnTimeThenstrISO=strISO&""&intHour&":"&intMin&":"&intSec 'Displaytheresult WScript.EchoTrim(strISO) FunctionDatePartLZ(myInterval,myDate) 'AddaleadingzerototheDatePart()ifnecessary DimstrDatePart strDatePart=DatePart(myInterval,myDate) IfLen(strDatePart)<2ThenstrDatePart="0"&strDatePart DatePartLZ=strDatePart EndFunction SubSyntax WScript.Echovbcrlf_ &"Date2ISO.vbs,Version1.02"_ &vbCrLf_ &"Convertanydate/timetoISOdate/time"_ &vbCrLf&vbCrLf_ &"Usage:CSCRIPT.EXE//NoLogoDate2ISO.vbsdate[time][/D|/T]"_ &vbCrLf&vbCrLf_ &"Where:""date""isthedatetoconvert(default:currentdate/time)"_ &vbCrLf_ &"""time""istheoptionaltimetoconvert"_ &vbCrLf_ &"/Dreturndateonly(default:bothdateandtime)"_ &vbCrLf_ &"/Treturntimeonly(/Dand/Taremutuallyexclusive)"_ &vbCrLf&vbCrLf_ &"Note:Ifthespecifieddateisambiguous,thecurrentuser'sdate"_ &vbCrLf_ &"andtimeformatisassumed."_ &vbCrLf&vbCrLf_ &"WrittenbyRobvanderWoude"_ &vbCrLf_ &"http://www.robvanderwoude.com" WScript.Quit1 EndSub
附上一段VBS校对系统时间的代码给大家参考下
'VBS校准系统时间BYBatMan DimobjXML,Url,Message Message="恭喜你,本机时间非常准确无需校对!" SetobjXML=CreateObject("MSXML2.XmlHttp") Url="http://open.baidu.com/special/time/" objXML.open"GET",Url,False objXML.send() DoUntilobjXML.readyState=4:WScript.Sleep200:Loop DimobjStr,LocalDate objStr=objXML.responseText LocalDate=Now() SetobjXML=Nothing DimobjREG,regNum SetobjREG=NewRegExp objREG.Global=True objREG.IgnoreCase=True objREG.Pattern="window.baidu_time\((\d{13,})\)" regNum=Int(objREG.Execute(objStr)(0).Submatches(0))/1000 DimOldDate,BJDate,Num,Num1 OldDate="1970-01-0108:00:00" BJDate=DateAdd("s",regNum,OldDate) Num=DateDiff("s",LocalDate,BJDate) IfAbs(Num)>=1Then DimDM,DT,TM,objSHELL DM=DateAdd("S",Num,Now()) DT=DateValue(DM) TM=TimeValue(DM) IfInStr(Now,"午")Then DimArr,Arr1,h24 Arr=Split(TM,"") Arr1=Split(Arr(1),":") h24=Arr1(0) IfArr(0)="下午"Then h24=h24+12 Else Ifh24=12Thenh24=0 EndIf TM=h24&":"&Arr1(1)&":"&Arr1(2) EndIf SetobjSHELL=CreateObject("Wscript.Shell") objSHELL.Run"cmd/cdate"&DT,False,True objSHELL.Run"cmd/ctime"&TM,False,True Num1=Abs(DateDiff("s",Now(),BJDate)) Message="【校准前】"&vbCrLf_ &"标准北京时间为:"&vbTab&BJDate&vbCrLf_ &"本机系统时间为:"&vbTab&LocalDate&vbCrLf_ &"与标准时间相差:"&vbTab&Abs(Num)&"秒"&vbCrLf&vbCrLf_ &"【校准后】"&vbCrLf_ &"本机系统时间为:"&vbTab&Now()&vbCrLf_ &"与标准时间相差:"&vbTab&Num1&"秒" SetobjSHELL=Nothing EndIf WScript.EchoMessage
以上所述就是本文的全部内容了,希望对大家学习VBS能够有所帮助。