Lesson 4: Working with time and dates

In this lesson, we will try to look at the many different options for working with time and dates in ASP. We went through two very simple functions in the previous lesson mostly to show you what ASP is. In this lesson, we will look at some more functions and advanced techniques.

Time and date functions

VBScript contains three important functions that return the current time and/or date on the server:

Returns the current date and time on the server.
Returns the current date on the server.
Returns the current time on the server.

This example illustrates the three functions:

	<title>Time and date</title>

	Response.Write "<p>The function Now returns: " & Now & "</p>"

	Response.Write "<p>The function Date returns: " & Date & "</p>"

	Response.Write "<p>The function Time returns: " & Time & "</p>"


Formatting time and dates

As you've probably noticed, the functions returns the date and time in some very "raw" formats. But there are, of course, different ways to format both the date and time.

The option we want to cover in this lesson is based on the function DocumentationFormatDateTime. In a later lesson, we will make our own function that can return exactly the format we want. But first, let us look at the predefined options in VBScript.

Returns a time in specified format

FormatDateTime is a function exactly like the functions above but the function FormatDateTime must be called with arguments. This means that you call the function in the following form:

	FormatDateTime(Date[, NamedFormat])

The NamedFormat argument may take the following values:

  • vbLongDate
  • vbShortDate
  • vbLongTime
  • vbShortTime

Let us look at an example that shows the different date and time formats. In the example below, we call the function with DocumentationNow. But it can be called with any date and time (eg. "14-02-2011 12:34:01")

	<title>time and date</title>



	<dd><% Response.Write FormatDateTime(Now, vbLongDate) %></dd>

	<dt>vbShortDate </dt>
	<dd><% Response.Write FormatDateTime(Now, vbShortDate) %></dd>


	<dd><% Response.Write FormatDateTime(Now, vbLongTime) %></dd>

	<dd><% Response.Write FormatDateTime(Now, vbShortTime) %></dd>


	Response.Write "Today it is " & datetime format (Now, vbLongDate)
	Response.Write " and the time is " & Format datetime (Now, vbShortTime)



More functions related to date and time

There are a number of other functions that return parts of a date or time. They are all called with a date or time as an argument. We will not go into depth with them all here, but just show one example and briefly describe the others.

As an example, we look at the function DocumentationMinute which returns a number corresponding to the minutes in the argument (the specified time). The function is called in the following form:


Therefore, to find out what minute we're in right now, we write:


The function returns the number 12 - which is exactly the minutes in the time right now: 01:12:29

The following functions relate similarly to dates or times. As an example they are called with DocumentationNow (23-04-2024 01:12:29)

Returns the current year from a date - with today's date, it returns: 2024
Returns the current month from a date - with today's date, it returns: 4
Returns the current day of the month from a date - with today's date, it returns: 23
Returns the current day of the week from a date - with today's date, it returns: 2
NOTE: this function has to be called with the argument "the first day of the week" (eg. Monday or Sunday) as well
- like this: Weekday(Now,vbMonday)
Returns the current hour from a time - with the current time, it returns: 1
Returns the current minute from a time - with the current time, it returns: 12
Returns the current second from a time - with the current time, it returns: 29

What can you use it for?

All this may seem a bit theoretical at this stage. After all, what on earth can you use a function like DocumentationSecond for? More importantly, when will you learn something you can actually use on your pages?

The answer is that what you learn here are building blocks - the only limitations to what you can build are your creativity and imagination! I would venture to say that you have already learned more than you think. For example, do you think you can make a website with different background images each day of the week and that works in all browsers?

Sure you can! Look at this example:

	<title>time and date</title>

	<body background="background_<% =Weekday(Now,vbMonday) %> .png">


The example above, with a dynamic background image, simply requires that you make seven images and name them background_1.png, background_2.png, background_3.png, etc.

If a user then enters your site on a Tuesday the site will have background_2.png as background, and the next day, background_3.png. Easy and simple!.

In the next lesson, you will be introduced to new building blocks that can be used to make loops and repetitions in your codes.

ASP is fun, don't you think?

<< Lesson 3: Your first ASP page

Lesson 5: Loops >>