Code _vbs to modify (set) system time and date with VBS

Source: Internet
Author: User
Tags time and date
Talking to someone about y2k38 the other day, I thought of a prank: using VBS to change the system to January 19, 2038 3:14 07 seconds, so some programs that rely on the Unix timestamp will go wrong. So how do you change the time of the system with the VBS?


The simplest and least technical method is to invoke the date and time command for CMD:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2011/4/27
Dim WshShell
Set WshShell = CreateObject ("WScript. Shell ")
Wshshell.run "cmd.exe/c date 2038-01-19", 0
Wshshell.run "cmd.exe/c time 3:14:08", 0

A more technical approach is to use the SetDateTime method of the WMI Win32_OperatingSystem class:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2011/4/27
Dtmnewdatetime = "20380119031408.000000+480" ' UTC time
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:{(Systemtime)}\\" & StrComputer & "\root\cimv2")
Set coloses = objWMIService.ExecQuery ("SELECT * from Win32_OperatingSystem")
For each objos in coloses
Objos.setdatetime Dtmnewdatetime
Next

Windows 7 requires administrator privileges to change the time when UAC is turned on, which is no fun at all.

Reference Links: Hey, scripting guy!. How Can I Set the "Date and Time" on a Computer?

From: http://demon.tw/programming/vbs-modify-system-time.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.