-
VBS显示当前标准时间
本文给大家分享的是使用vbs来显示当前时间的2个实例,非常的简单实用,有需要的小伙伴可以参考下。
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能够有所帮助。
来源:https://www.jb51.net/article/66264.htm
最新更新
VBScript中On Error语句用法小结
VBS显示当前标准时间
VBS实现将当前时间转换成UTC时间
VBS脚本写的Windows硬件检测工具分享
vb下的恶搞关机程序
vbs脚本实现修改DNS和清空IE临时文件功能
使用Java实现KMZ和KML数据的直接解析
Spring Boot最经典的20道面试题你都会了吗
Java中Instant的使用及转换
Spring中ClassPath指的是哪些地方
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比