To read the article online, visit

Date Functions

There are some inherent ASP date functions which are very powerful. They are very similar to the inherent SQL date functions bearing the same name, although the syntax varies slightly.

To get the current date, you can use the Date keyword. To get the current date and time, use the Now keyword. Here is a line of code that would print out the current date:

Response.Write("Today is " & Date)

To get the month, year, or day of any date, you can use the built in functions Month, Year, or Day. For example, if today were the 4th of December, 1998 (as today happens to be), here is how you could print out 12/4/1998:

Response.Write(Month(Date) & "/" & Day(Date) & "/" & Year(Date))

One of the most useful date functions is DateSerial. DateSerial allows you to create a date from three parameters: the year, month, and day. While this may not seem that spectacular, it is, and you will see why in a moment.

Let's look at a quick line of code which would create a date of 3/14/98 and store it in a variable name SomeDate:

Dim SomeDate
SomeDate = DateSerial(1998, 3, 14)

As you can probably see, DateSerial expects first the year, then the month, then the date. If we were to print out SomeDate, it would appear as "3/14/98" (or perhaps different, based on your machines regional settings).

Now, for the real power with DateSerial. Let's say you want to make the variable SomeDate equal to the last day of the month two months ago from today. This could be a very tricky operation, because what if the current month was January, then you would have to go back a year. If the current month was April, you'd have to worry if it was a leap year or not. However, with DateSerial, none of these worries arise. Let's look at a line of code which will set SomeDate equal to the date described above:

SomeDate = DateSerial(Year(Date), Month(Date) - 1, 1 - 1)

You may be furrowing your brow as you read this, but at least let me try to explain first. The DateSerial is forming a date for the current year (Year(Date)), for the month one month before the current month (i.e. the Month(Date) - 1, for the first day before the first day of the month (which is the last day of the month before the month before the current month. If that sentence doesn't have you confused, let me try once again.

The above syntax is giving you...

The first day before the first day of the month before the month before the current month for the current year.

If I didn't confuse you by now you are truly a wiz.

Article Information
Article Title: Date Functions
Article Author: Scott Mitchell
Article URL:

Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers