VBScript date and time
VBScript provides the functionality to display date and time on a webpage.
NOTE: Whenever you display the date or time on a webpage using VBScript, it will get that data from whatever the user has on their computer, whether it is correct or not. So if the current date and time is January 15th 12:30, but the users computer is set to January 10th 8:30, that is what will be displayed.
NOTE: To see the output for the examples on this page look at the page in Internet Explorer or on Firefox using the IETab extension.
This tutorial focuses on:
- VBScript date functions
- VBScript time functions
- Displaying a date on a webpage
- Displaying time on a webpage
- Displaying date and time on a webpage
- Displaying custom formatted date and time
VBScript date functions
VBScript provides several functions for displaying a date on a webpage.
The Date function
The Date function is used to return the current date.
The Day() function
The date parameter is any variable or function that represents a date. This function will return the day of the month (in numerical form from 1 to 31) from the date specified by the date parameter.
The Month() function
The date parameter is any variable or function that represents a date. This function will return the current month (in numerical form from 1 to 12) from the date specified by the date parameter.
The Year() function
The date parameter is any variable or function that represents a date. This function will return the current year (in numerical form (4 digits)) from the date specified by the date parameter.
The IsDate() function
The date parameter is any expression or function that can be checked if it is a date or not. This function will return a boolean value (true/false) signifying if date is a date or not.
VBScript time functions
VBScript provides several functions for displaying time on a webpage.
The Time function
The Time function is used to return the current time.
The Minute() function
The time parameter is any variable or function that represents time. This function will return the current minute (from 0 to 59) from the time specified by the time parameter.
The Hour() function
The time parameter is any variable or function that represents time. This function will return the current hour (from 0 to 23) from the time specified by the time parameter.
The Second() function
The time parameter is any variable or function that represents time. This function will return the current second (from o to 59) of the current minute from the time specified.
The Timer function
The Timer function is used to return the number of seconds that have elapsed since midnight (12 a.m.) for the current day.
Displaying a date on a webpage
You can display a date on a webpage using the above mentioned date functions:
Displaying time on a webpage
You can display time on a webpage using the above mentioned time functions:
Displaying date and time on a webpage
You can display the date and time together on a webpage using the Now function:
Displaying custom formatted date and time
VBScript displays date and time in one format by default but you can change this using the FormatDateTime() function.
The dateOrTime parameter is the date or time you want to format, and the format parameter is the type of formatting to apply.
The format parameter can take a value from 0 - 4, each value signifying a different type of formatting:
0 (used for dates) - display a date in the format mm/dd/yy (default)
1 (used for dates) - display a date in the format day, month and month day, year
2 (used for dates) - display a date in the format mm/dd/yy (default (same as 0))
3 (used for time) - display time in the format hh:mm:ss: PM/AM (12 hour format)
4 (used for time) - display time in the format hh:mm (24 hour format)