Working With Dates and Times in VBScript - Date formats
(Page 6 of 6 )
VBScript provides a cool function for controlling the display format of dates and times. Its sole purpose is to return the string representation of a date or time in a specified format.
WScript.Echo FormatDateTime(Now)
WScript.Echo FormatDateTime(Now, vbLongDate)
WScript.Echo FormatDateTime(Now, vbShortDate)
WScript.Echo FormatDateTime(Now, vbLongTime)
WScript.Echo FormatDateTime(Now, vbShortTime)
Output:
9/5/2008 5:31:14 AM
Friday, September 05, 2008
9/5/2008
5:31:14 AM
05:31
The FormatDateTime function has one required parameter, a Date to format. A second optional parameter is used to determine the format that should be outputted based on a constant value in the table below. If omitted, vbGeneralDate is used.
Table 5: VBScript Date Format Constants
Constant | Value | Description |
vbGeneralDate | 0 | Display a date in short date format. If the date parameter is Now(), it will also return the time, after the date. |
vbLongDate | 1 | Display a date using the local long date format. E.g. Weekday, Month, Day, Year |
vbShortDate | 2 | Display a date using the local short date format. E.g. MM/DD/YY |
vbLongTime | 3 | Display a time using the local time format. E.g. hh:mm:ss AM/PM |
vbShortTime | 4 | Display a time using the 24-hour format: hh:mm. |
As I stated earlier in this article, VBScript supports nearly every valid date and time format; however, its default types are determined by the regional date and time settings of the local system. From type to time, you may want to change these settings. They can be changed very easily from within VBScript.
WScript.Echo GetLocale()
SetLocale(1033)
SetLocale("en-us")
Ouput:
1033
The GetLocale function returns an integer representing the current regional code. The SetLocale function can be used to set the current regional code by either numeric code or its string abbreviation. Note that the SetLocale function globally affects the local system settings.
'Start of script:
intRegionalCode = GetLocale()
SetLocale("en-gb")
'End of script:
SetLocale(intRegionalCode)
The example above demonstrates how you can use the GetLocale and SetLocale functions to temporarily change the local date and time region for the duration of your script.
You now have all the tools you need to begin working with dates and times in VBScript. In my next article, I’ll show you how to put these functions to the test by creating a dynamic calendar. Stick around for a chance to learn by example. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |