In the previous FAQ, I discussed how one could use DateSerial to form a date. In this FAQ I will discuss a few other, powerful date functions. DatePart is a useful date function, because it allows you to display a "part" of a date. For example, we can print out the month for a date by using code like this:
DatePart("m", DateSerial(1998, 11, 4));
This will return 11. The DatePart function takes up to four properties:
DatePart(interval, date[, firstdayofweek[, firstdayofyear]])
Interval can be any one of the following values (in quotes)
| Setting | Description |
|---|---|
| yyyy | Year |
| q | Quarter |
| m | Month |
| y | Day of Year |
| d | Day |
| w | Weekday |
| ww | Week of Year |
| h | Hour |
| n | Minute |
| s | Second |
The optional firstdayofweek parameter specifies what day is
the first day of the week. The default is Sunday (specified by a 1). If
you want the system to count Saturday as the first day of the week, you
would pass in firstdayofweek as 7, since Saturday is normally
the 7th day of the week. (Sunday = 1, Monday = 2, ..., Saturday = 7.)
Yet another powerful date function is DateAdd, which adds a certain interval to a date. The intervals are the same as above for the DatePart. Here is the specification for DateAdd:
DateAdd(interval, number, date)
Using DateAdd you could construct a date two months in the future. Let's say that we wanted to print out the day of the week exactly a year ago from today. No problem, all we need to use is DateAdd and DatePart.
DatePart("w", DateAdd("y", -1, Date))
Notice that you can use DateAdd to sort of subtract from a date by using a negative number. Date functions are definitely powerful, useful, and easy to use in ASP.
Related Articles:
| FAQ Table of Contents |
|
Passing Variables Between ASP Pages |




