温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VBS怎么实现显示当前标准时间

发布时间:2021-08-02 16:21:34 来源:亿速云 阅读:141 作者:chen 栏目:开发技术

本篇内容介绍了“VBS怎么实现显示当前标准时间”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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, h34 
Arr = Split(TM, " ") 
Arr1 = Split(Arr(1), ":") 
h34 = Arr1(0) 
If Arr(0) = "下午" Then 
h34 = h34 + 12 
Else 
If h34 = 12 Then h34 = 0 
End If 
TM = h34 & ":" & 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怎么实现显示当前标准时间”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vbs
AI