• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    VBS显示当前标准时间

    发布者: 竹韵9933 | 发布时间: 2025-8-13 22:46| 查看数: 34| 评论数: 0|帖子模式

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

    2. Dim blnDate, blnTime
    3. Dim dtmDate
    4. Dim intDay, intFormat, intHour, intMin, intMonth, intSec, intUTC, intValid, intYear
    5. Dim strISO

    6. With WScript.Arguments
    7.   ' Check command line arguments
    8.   If .Unnamed.Count = 0 Then dtmDate = Now
    9.   If .Unnamed.Count > 0 Then dtmDate = .Unnamed(0)
    10.   If .Unnamed.Count > 1 Then dtmDate = dtmDate & " " & .Unnamed(1)
    11.   If .Unnamed.Count > 2 Then dtmDate = dtmDate & " " & .Unnamed(2)
    12.   If .Unnamed.Count > 3 Then Syntax
    13.   On Error Resume Next
    14.   dtmDate = CDate( dtmDate )
    15.   If Err Then
    16.     On Error Goto 0
    17.     Syntax
    18.   End If
    19.   On Error Goto 0
    20.   If Not IsDate( dtmDate ) Then Syntax
    21.   intValid = 0
    22.   blnDate = True
    23.   blnTime = True
    24.   If .Named.Exists( "D" ) Then
    25.     blnDate = True
    26.     blnTime = False
    27.     intValid = intValid + 1
    28.   End If
    29.   If .Named.Exists( "T" ) Then
    30.     blnDate = False
    31.     blnTime = True
    32.     intValid = intValid + 1
    33.   End If
    34.   If intValid <> .Named.Count Then Syntax
    35.   If intValid > 1 Then Syntax
    36. End With

    37. ' Format the output string
    38. intYear = DatePartLZ( "yyyy", dtmDate )
    39. intMonth = DatePartLZ( "m", dtmDate )
    40. intDay  = DatePartLZ( "d", dtmDate )
    41. intHour = DatePartLZ( "h", dtmDate )
    42. intMin  = DatePartLZ( "n", dtmDate )
    43. intSec  = DatePartLZ( "s", dtmDate )
    44. If blnDate Then strISO = intYear & "-" & intMonth & "-" & intDay
    45. If blnTime Then strISO = strISO & " " & intHour & ":" & intMin & ":" & intSec
    46. ' Display the result
    47. WScript.Echo Trim( strISO )


    48. Function DatePartLZ( myInterval, myDate )
    49.   ' Add a leading zero to the DatePart() if necessary
    50.   Dim strDatePart
    51.   strDatePart = DatePart( myInterval, myDate )
    52.   If Len( strDatePart ) < 2 Then strDatePart = "0" & strDatePart
    53.   DatePartLZ = strDatePart
    54. End Function


    55. Sub Syntax
    56.   WScript.Echo vbcrlf _
    57.         & "Date2ISO.vbs, Version 1.02" _
    58.         & vbCrLf _
    59.         & "Convert any date/time to ISO date/time" _
    60.         & vbCrLf & vbCrLf _
    61.         & "Usage: CSCRIPT.EXE //NoLogo Date2ISO.vbs date [ time ] [ /D | /T ]" _
    62.         & vbCrLf & vbCrLf _
    63.         & "Where: ""date""  is the date to convert (default: current date/time)" _
    64.         & vbCrLf _
    65.         & "    ""time""  is the optional time to convert" _
    66.         & vbCrLf _
    67.         & "    /D    return date only (default: both date and time)" _
    68.         & vbCrLf _
    69.         & "    /T    return time only (/D and /T are mutually exclusive)" _
    70.         & vbCrLf & vbCrLf _
    71.         & "Note:  If the specified date is ambiguous, the current user's date" _
    72.         & vbCrLf _
    73.         & "    and time format is assumed." _
    74.         & vbCrLf & vbCrLf _
    75.         & "Written by Rob van der Woude" _
    76.         & vbCrLf _
    77.         & "http://www.robvanderwoude.com"
    78.   WScript.Quit 1
    79. End Sub
    复制代码
    附上一段VBS校对系统时间的代码给大家参考下
    1. 'VBS校准系统时间 BY BatMan
    2. Dim objXML, Url, Message
    3. Message = "恭喜你,本机时间非常准确无需校对!"
    4. Set objXML = CreateObject("MSXML2.XmlHttp")
    5. Url = "http://open.baidu.com/special/time/"
    6. objXML.open "GET", Url, False
    7. objXML.send()
    8. Do Until objXML.readyState = 4 : WScript.Sleep 200 : Loop
    9. Dim objStr, LocalDate
    10. objStr = objXML.responseText
    11. LocalDate = Now()
    12. Set objXML = Nothing
    13. Dim objREG, regNum
    14. Set objREG = New RegExp
    15. objREG.Global = True
    16. objREG.IgnoreCase = True
    17. objREG.Pattern = "window.baidu_time\((\d{13,})\)"
    18. regNum = Int(objREG.Execute(objStr)(0).Submatches(0)) /1000
    19. Dim OldDate, BJDate, Num, Num1
    20. OldDate = "1970-01-01 08:00:00"
    21. BJDate = DateAdd("s", regNum, OldDate)
    22. Num = DateDiff("s", LocalDate, BJDate)
    23. If Abs(Num) >=1 Then
    24. Dim DM, DT, TM, objSHELL
    25. DM = DateAdd("S", Num, Now())
    26. DT = DateValue(DM)
    27. TM = TimeValue(DM)
    28. If InStr(Now, "午") Then
    29. Dim Arr, Arr1, h24
    30. Arr = Split(TM, " ")
    31. Arr1 = Split(Arr(1), ":")
    32. h24 = Arr1(0)
    33. If Arr(0) = "下午" Then
    34. h24 = h24 + 12
    35. Else
    36. If h24 = 12 Then h24 = 0
    37. End If
    38. TM = h24 & ":" & Arr1(1) & ":" & Arr1(2)
    39. End If
    40. Set objSHELL = CreateObject("Wscript.Shell")
    41. objSHELL.Run "cmd /cdate " & DT, False, True
    42. objSHELL.Run "cmd /ctime " & TM, False, True
    43. Num1 = Abs(DateDiff("s", Now(), BJDate))
    44. Message = "【校准前】" & vbCrLf _
    45. & "标准北京时间为:" & vbTab & BJDate & vbCrLf _
    46. & "本机系统时间为:" & vbTab & LocalDate & vbCrLf _
    47. & "与标准时间相差:" & vbTab & Abs(Num) & "秒" & vbCrLf & vbCrLf _
    48. & "【校准后】" & vbCrLf _
    49. & "本机系统时间为:" & vbTab & Now() & vbCrLf _
    50. & "与标准时间相差:" & vbTab & Num1 & "秒"
    51. Set objSHELL = Nothing
    52. End If
    53. WScript.Echo Message
    复制代码
    以上所述就是本文的全部内容了,希望对大家学习VBS能够有所帮助。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表