VBScript
VBScript introduction
VBScript basics
VBScript variables
VBScript procedures
VBScript popup boxes
VBScript conditions
VBScript loops
VBScript arrays
VBScript strings
VBScript date & time
VBScript summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

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: Remember that VBScript code only works in Internet Explorer or Internet Explorer based browsers such as SlimBrowser. If you are using any other browser to view this page, the VBScript code will not execute. If you are using Firefox, download the IETab extension to be able to view the output of code written in VBScript inside Firefox.

This lesson 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.

Syntax:

None. Use it as it is.

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 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("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 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 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 or 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.

Syntax:

None. Use it as it is.

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 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.

Syntax:

None. Use it as it is.

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:

<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:

<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:

<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:

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments