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:
<html> <head> <title>Time and date</title> </head> <body> <% 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>" %> </body> </html>
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 FormatDateTime. 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:
The NamedFormat argument may take the following values:
Let us look at an example that shows the different date and time formats. In the example below, we call the function with Now. But it can be called with any date and time (eg. "14-02-2011 12:34:01")
<html> <head> <title>time and date</title> </head> <body> <dl> <dt>vbLongDate</dt> <dd><% Response.Write FormatDateTime(Now, vbLongDate) %></dd> <dt>vbShortDate </dt> <dd><% Response.Write FormatDateTime(Now, vbShortDate) %></dd> <dt>vbLongTime</dt> <dd><% Response.Write FormatDateTime(Now, vbLongTime) %></dd> <dt>vbShortTime</dt> <dd><% Response.Write FormatDateTime(Now, vbShortTime) %></dd> </dl> <p> <% Response.Write "Today it is " & datetime format (Now, vbLongDate) Response.Write " and the time is " & Format datetime (Now, vbShortTime) %> </p> </body> </html>
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 Minute 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 16 - which is exactly the minutes in the time right now: 05:16:07
The following functions relate similarly to dates or times. As an example they are called with Now (16-01-2017 05:16:07)
- Returns the current year from a date - with today's date, it returns: 2017
- Returns the current month from a date - with today's date, it returns: 1
- Returns the current day of the month from a date - with today's date, it returns: 16
- Returns the current day of the week from a date - with today's date, it returns: 1
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: 5
- Returns the current minute from a time - with the current time, it returns: 16
- Returns the current second from a time - with the current time, it returns: 7
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 Second 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:
<html> <head> <title>time and date</title> </head> <body background="background_<% =Weekday(Now,vbMonday) %> .png"> </body> </html>
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?