欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS显示当前标准时间

程序员文章站 2022-06-25 08:58:55
vbs显示当前标准时间,例如:执行下面的代码则显示:2013-05-11 19:10:11 option explicit dim blndate, bl...

vbs显示当前标准时间,例如:执行下面的代码则显示:2013-05-11 19:10:11

option explicit
 
dim blndate, blntime
dim dtmdate
dim intday, intformat, inthour, intmin, intmonth, intsec, intutc, intvalid, intyear
dim striso
 
with wscript.arguments
  ' check command line arguments
  if .unnamed.count = 0 then dtmdate = now
  if .unnamed.count > 0 then dtmdate = .unnamed(0)
  if .unnamed.count > 1 then dtmdate = dtmdate & " " & .unnamed(1)
  if .unnamed.count > 2 then dtmdate = dtmdate & " " & .unnamed(2)
  if .unnamed.count > 3 then syntax
  on error resume next
  dtmdate = cdate( dtmdate )
  if err then
    on error goto 0
    syntax
  end if
  on error goto 0
  if not isdate( dtmdate ) then syntax
  intvalid = 0
  blndate = true
  blntime = true
  if .named.exists( "d" ) then
    blndate = true
    blntime = false
    intvalid = intvalid + 1
  end if
  if .named.exists( "t" ) then
    blndate = false
    blntime = true
    intvalid = intvalid + 1
  end if
  if intvalid <> .named.count then syntax
  if intvalid > 1 then syntax
end with
 
' format the output string
intyear = datepartlz( "yyyy", dtmdate )
intmonth = datepartlz( "m", dtmdate )
intday  = datepartlz( "d", dtmdate )
inthour = datepartlz( "h", dtmdate )
intmin  = datepartlz( "n", dtmdate )
intsec  = datepartlz( "s", dtmdate )
if blndate then striso = intyear & "-" & intmonth & "-" & intday
if blntime then striso = striso & " " & inthour & ":" & intmin & ":" & intsec
' display the result
wscript.echo trim( striso )
 
 
function datepartlz( myinterval, mydate )
  ' add a leading zero to the datepart() if necessary
  dim strdatepart
  strdatepart = datepart( myinterval, mydate )
  if len( strdatepart ) < 2 then strdatepart = "0" & strdatepart
  datepartlz = strdatepart
end function
 
 
sub syntax
  wscript.echo vbcrlf _
        & "date2iso.vbs, version 1.02" _
        & vbcrlf _
        & "convert any date/time to iso date/time" _
        & vbcrlf & vbcrlf _
        & "usage: cscript.exe //nologo date2iso.vbs date [ time ] [ /d | /t ]" _
        & vbcrlf & vbcrlf _
        & "where: ""date""  is the date to convert (default: current date/time)" _
        & vbcrlf _
        & "    ""time""  is the optional time to convert" _
        & vbcrlf _
        & "    /d    return date only (default: both date and time)" _
        & vbcrlf _
        & "    /t    return time only (/d and /t are mutually exclusive)" _
        & vbcrlf & vbcrlf _
        & "note:  if the specified date is ambiguous, the current user's date" _
        & vbcrlf _
        & "    and time format is assumed." _
        & vbcrlf & vbcrlf _
        & "written by rob van der woude" _
        & vbcrlf _
        & "http://www.robvanderwoude.com"
  wscript.quit 1
end sub

附上一段vbs校对系统时间的代码给大家参考下

'vbs校准系统时间 by batman 
dim objxml, url, message 
message = "恭喜你,本机时间非常准确无需校对!" 
set objxml = createobject("msxml2.xmlhttp") 
url = "http://open.baidu.com/special/time/" 
objxml.open "get", url, false 
objxml.send() 
do until objxml.readystate = 4 : wscript.sleep 200 : loop 
dim objstr, localdate 
objstr = objxml.responsetext 
localdate = now() 
set objxml = nothing 
dim objreg, regnum 
set objreg = new regexp 
objreg.global = true 
objreg.ignorecase = true 
objreg.pattern = "window.baidu_time\((\d{13,})\)" 
regnum = int(objreg.execute(objstr)(0).submatches(0)) /1000 
dim olddate, bjdate, num, num1 
olddate = "1970-01-01 08:00:00" 
bjdate = dateadd("s", regnum, olddate) 
num = datediff("s", localdate, bjdate) 
if abs(num) >=1 then 
dim dm, dt, tm, objshell 
dm = dateadd("s", num, now()) 
dt = datevalue(dm) 
tm = timevalue(dm) 
if instr(now, "午") then 
dim arr, arr1, h24 
arr = split(tm, " ") 
arr1 = split(arr(1), ":") 
h24 = arr1(0) 
if arr(0) = "下午" then 
h24 = h24 + 12 
else 
if h24 = 12 then h24 = 0 
end if 
tm = h24 & ":" & arr1(1) & ":" & arr1(2) 
end if 
set objshell = 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 & "秒" 
set objshell = nothing 
end if 
wscript.echo message 

以上所述就是本文的全部内容了,希望对大家学习vbs能够有所帮助。