Menu Close

VBScript – FormatDateTime

This article will run through some examples on how to use VBScript to change a date and / or time into different formats.

The function that makes this easier is called FormatDateTime .

PLEASE NOTE:

The format of the date and time can vary from one computer to the next because it is displayed as per settings in the "Regional and Language Options" in the "Control Panel".

Syntax:

FormatDateTime (Date, Format)

Date:

Any valid date expression

Format:

vbGeneralDate -OR- 0: Short date and long time

vbLongDate -OR- 1: Long date

vbShortDate -OR- 2: Short date

vbLongTime -OR- 3: Long time

vbShortTime -OR- 4: Short time (24-hour format)

WScript.Echo FormatDateTime (Now (), vbGeneralDate) -OR-
WScript.Echo FormatDateTime (Now (), 0)

Sample return "2006/09/12 10:10:28 PM"

WScript.Echo FormatDateTime (Now (), vbLongDate) -OR-
WScript.Echo FormatDateTime (Now (), 1)

Sample return "12 September 2006"

WScript.Echo FormatDateTime (Now (), vbShortDate) -OR-
WScript.Echo FormatDateTime (Now (), 2)

Sample return "2006/09/12"

WScript.Echo FormatDateTime (Now (), vbLongTime) -OR-
WScript.Echo FormatDateTime (Now (), 3)

Sample return "10:14:58 PM"

WScript.Echo FormatDateTime (Now (), vbShortTime) -OR-
WScript.Echo FormatDateTime (Now (), 4)

Sample return "12 September 2006"

PLEASE NOTE:

If you replace Now () with a fixed date without a time, in each case where normally a time is shown, it will be omitted (show only a date) and where only a time is shown (eg. VbShortTime) it will return " 12 o'clock".

WScript.Echo FormatDateTime ("2006/09/12", vbShortTime)

Sample return "00:00"


Source by Shaun Vermaak

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.