脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - VBS - VBS显示当前标准时间

VBS显示当前标准时间

2020-08-16 14:04VBS教程网 VBS

本文给大家分享的是使用vbs来显示当前时间的2个实例,非常的简单实用,有需要的小伙伴可以参考下。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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校对系统时间的代码给大家参考下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'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能够有所帮助。

延伸 · 阅读

精彩推荐
  • VBSVBS文本文件操作实现代码

    VBS文本文件操作实现代码

    这篇文章主要介绍了VBS文本文件操作实现代码,需要的朋友可以参考下...

    VBS代码网8912020-08-12
  • VBSWINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

    WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

    本文主要分享WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码,有需要的童鞋可以参考下...

    脚本之家3232020-07-06
  • VBSAdsutil.vbs 在脚本攻击中的妙用[我非我原创]

    Adsutil.vbs 在脚本攻击中的妙用[我非我原创]

    adsutil.vbs是什么?相信用过IIS的网管员不会不知道。这是IIS自带的提供于命令行下管理IIS的一个脚本。位于%SystemDrive%\Inetpub\AdminScripts目录下。...

    VBS教程网2312020-07-07
  • VBS雷客图ASP站长安全助手vbs测试版代码

    雷客图ASP站长安全助手vbs测试版代码

    雷客图ASP站长安全助手是一个基于ASP的帮助站长维护网站安全的程序。这个版本(vbs测试版)主要用于服务器本地运行以查找ASP木马。此版本为测试版,希...

    脚本之家2312020-07-03
  • VBSVBS编程教程 (第1篇)

    VBS编程教程 (第1篇)

    VBScript的全称是:Microsoft Visual Basic Script Editon.(微软公司可视化BASIC脚本版). 正如其字面所透露的信息, VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言....

    VBS教程网4812020-07-15
  • VBSVBS数组深入浅出

    VBS数组深入浅出

    VBS数组在应用中没有像其他语句那么广泛,VBS数组存在不少功能上的局限性(如二维数组的定义、赋值),在使用上也没有java等语言那么便捷...

    VBS教程网2962020-08-18
  • VBS灵活实用VBS入门教程应用篇

    灵活实用VBS入门教程应用篇

    上一篇文章我们了解了VBS编程的一些基础知识,要更深入地学习还要学习选择结构和循环结构。 ...

    VBS教程网5742020-07-01
  • VBSVBS教程:正则表达式简介 -正则表达式语法

    VBS教程:正则表达式简介 -正则表达式语法

    正则表达式语法 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的...

    VBS教程网3302019-11-25