The function code of getting UNIX time stamp with vbs

Time:2019-12-24

There is no time function similar to C standard library in VBS. How to get UNIX timestamp? At first glance, it’s simple:

Copy codeThe code is as follows:
Function UnixTime()
UnixTime = DateDiff(“s”, “01/01/1970 00:00:00”, Now())
End Function

A very natural way is to only notice “0:00:00 on January 1, 1970” and ignore “coordinated universal time”.

Coordinated universal time, also known as world standard time or world coordinated time, or UTC for short, comes from the English “coordinated universal time”. When the local time in mainland China is 8 hours faster than UTC, UTC + 8 will be written. If the local time is slower than UTC time, for example, Hawaii time is 10 hours slower than UTC time, utc-10 will be written.

The time returned by the now() function in VBS includes the time zone, so it needs to be corrected to get the UNIX time stamp.

Copy codeThe code is as follows:
Function UnixTime()
Set objWMIService = _
GetObject(“winmgmts:\\.\root\cimv2”)
Set colItems = objWMIService.ExecQuery _
(“Select * from Win32_OperatingSystem”,,48)
For Each objItem in colItems
TimeZone = objItem.CurrentTimeZone
Next
UnixTime = DateDiff(“s”, “01/01/1970 00:00:00”, Now())
UnixTime = UnixTime – TimeZone * 60
End Function

That’s the right way to write.
Reference link:Epoch & Unix Timestamp Conversion Tools
Original: http://demon.tw/programming/vbsunix-time-stamp.html

Recommended Today

Win11 Microsoft logo certification, Microsoft accepted and submitted

The company can provide windows 11Logo certification, WHQL, driver signature service. Windows 11, version 21H2 hardware driver submissions for Windows Hardware Compatibility Program (WHCP) are now being accepted at the Partner Center for Windows Hardware. The updated version of the Windows Hardware Lab Kit (HLK), along with updated playlists for testing Windows 11, version 21H2 […]