Bookmark and Share

VBScript Date Functions

This tutorial will teach you how to use VBScript's Date function and VBScript's Date Format Functions. Both of these functions are included with VBScript. Note: You will need to be running Internet Explorer to see the displays in this tutorial. This is because VBScript is a Microsoft only scripting language.

Advertise on Tizag.com

VBScript Date Function

The VBScript Date function is used to create the current date based off of the visitor's system time. This means that if a user from China and a visitor from America visit your site at the same time they will see their own local time displayed.

This example shows how you would get the current date, store it into a variable and print it to the browser.

VBScript Code:

<script type="text/vbscript">
Dim myDateString
myDateString = Date()
document.write(myDateString)
</script>

Display:

Note: You will only see the Display if you are viewing this web page with Internet Explorer.

VBScript FormatDateTime Function

If the above VBScript Date Format is not to your liking you can use the format constants of the FormatDateTime function to customize your Date display. VBScript has 3 constants that relate to the Date function:

  • 0 - Default setting. You can see this in the example above.
  • 1 - A long date defined by the computer's regional settings.
  • 2 - A short date defined by the regional settings.

We will be formatting the current date with these options, but you can also format static dates (i.e. January 1, 1500) by replacing Date() with your own date string.

VBScript Code:

<script type="text/vbscript">
Dim myDateString0, myDateString1, myDateString2, break
myDateString0 = FormatDateTime(Date(), 0)
myDateString1 = FormatDateTime(Date(), 1)
myDateString2 = FormatDateTime(Date(), 2)

break = "<br />"

document.write(myDateString0 & break)
document.write(myDateString1 & break)
document.write(myDateString2 & break)
</script>

Display:

Note: You will only see the Display if you are viewing this web page with Internet Explorer.

Bookmark and Share




Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials