VBScript
  1. VBScript intro
  2. VBScript basics
  3. VBScript variables
  4. VBScript procedures
  5. VBScript popup boxes
  6. VBScript conditions
  7. VBScript loops
  8. VBScript arrays
  9. VBScript strings
  10. VBScript date & time
  11. VBScript for IE 11
  12. VBScript summary

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 provides several functions for displaying a date on a webpage.

The Date function

The Date function is used to return the current date.

Example:
<script type="text/vbscript"> document.write("The current date is " & Date) </script>
Output:

The Day() function

Syntax:
Day(date)

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.

Example:
<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current day of the month from the current date document.write("<br />The current day of the month is " & Day(Date)) </script>
Output:

The Month() function

Syntax:
Month(date)

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.

Example:
<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current month from the current date document.write("The current month is " & Month(Date)) </script>
Output:

The Year() function

Syntax:
Year(date)

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.

Example:
<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") 'get the current year from the current date document.write("The current year is " & Year(Date)) </script>
Output:

The IsDate() function

Syntax:
IsDate(date)

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.

Example:
<script type="text/vbscript"> document.write(IsDate(Date) & "<br />") document.write(IsDate("Apple") & "<br />") document.write(IsDate("January 1, 1900") & "<br />") document.write(IsDate("January first nineteen hundred")) </script>
Output:

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.

Example:
<script type="text/vbscript"> document.write("The time is now " & Time) </script>
Output:

The Minute() function

Syntax:
Minute(time)

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.

Example:
<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") 'get the current minute from the time specified document.write("The current minute is " & Minute(Time)) </script>
Output:

The Hour() function

Syntax:
Hour(time)

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.

Example:
<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") 'get the current hour from the time specified document.write("The current hour is " & Hour(Time)) </script>
Output:

The Second() function

Syntax:
Second(time)

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.

Example:
<script type="text/vbscript"> <!-- document.write("Current time: " & Time & "<br />") 'get the current second from the current minute from the time specified document.write("The current second is " & Second(Time)) --> </script>
Output:

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.

Example:
<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") document.write("Number of seconds elapsed in the current day: " & Timer) </script>
Output:

Displaying a date on a webpage

You can display a date on a webpage using the above mentioned date functions:

Example:
<script type="text/vbscript"> document.write("Current date: " & Date & "<br />") document.write("Current month: " & Month(Date) & "<br />") document.write("Current day: " & Day(Date) & "<br />") document.write("Current year: " & Year(Date) & "<br />") document.write("'January first eighteen hundred' a valid date format: " & IsDate("January first eighteen hundred")) </script>
Output:

Displaying time on a webpage

You can display time on a webpage using the above mentioned time functions:

Example:
<script type="text/vbscript"> document.write("Current time: " & Time & "<br />") document.write("Current hour: " & Hour(Time) & "<br />") document.write("Current minute: " & Minute(Time) & "<br />") document.write("Current second: " & Second(Time) & "<br />") document.write("Number of seconds elapsed since current day began: " & Timer) </script>
Output:

Displaying date and time on a webpage

You can display the date and time together on a webpage using the Now function:

Example:
<script type="text/vbscript"> document.write("Date and time: " & Now) </script>
Output:

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.

Syntax:
FormatDateTime(dateOrTime, format)

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)

Example:
<script type="text/vbscript"> document.write("Current date: " & FormatDateTime(Date, 0) & "<br />") document.write("Current date: " & FormatDateTime(Date, 1) & "<br />") document.write("Current date: " & FormatDateTime(Date, 2) & "<br />") document.write("Current time: " & FormatDateTime(Time, 3) & "<br />") document.write("Current time: " & FormatDateTime(Time, 4)) </script>
Output:
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information