Get Current Date
This document provides details and examples for a simple Windows Script Host (WSH) script to retrieve and display the current date.
Script Description
The following VBScript code demonstrates how to access the current system date and format it for display. This script is commonly used for logging, timestamping operations, or providing user-friendly date information.
VBScript Example
' Get the current date object
Dim currentDate
currentDate = Date
' Display the date in a message box
MsgBox "The current date is: " & currentDate, vbInformation, "Current Date"
' You can also display it in the console if running via cscript
' WScript.Echo "The current date is: " & currentDate
Explanation
Dim currentDate
: Declares a variable namedcurrentDate
to store the date value.currentDate = Date
: The built-inDate
function in VBScript returns the current system date.MsgBox "The current date is: " & currentDate, vbInformation, "Current Date"
: Displays the retrieved date in a pop-up message box.vbInformation
is a constant that specifies the icon to display in the message box (an information icon)."Current Date"
is the title of the message box.
WScript.Echo "The current date is: " & currentDate
: (Commented out in the example) This line shows how to output the date to the console when the script is run usingcscript.exe
.
How to Run the Script
- Open Notepad or any plain text editor.
- Copy and paste the VBScript code above into the editor.
- Save the file with a
.vbs
extension (e.g.,GetDate.vbs
). - Double-click the saved file to run it. A message box will appear displaying the current date.
- Alternatively, open a Command Prompt, navigate to the directory where you saved the file, and type
cscript GetDate.vbs
to run it in the console.
Output Example
When the script is executed, a message box will appear similar to this:
(The date shown will be the actual current date when the script is run.)
Note: The format of the date displayed by the Date
function is dependent on your system's regional settings.
Further Customization
You can use other VBScript functions to format the date differently, such as:
FormatDateTime(Date, vbLongDate)
: Displays the date in the long date format (e.g., Friday, October 27, 2023).FormatDateTime(Date, vbShortDate)
: Displays the date in the short date format (e.g., 10/27/2023).FormatDateTime(Date, vbGeneralDate)
: Displays the date and time in the general date format.
For more advanced scripting needs, consider exploring PowerShell for Windows environments.