To read the article online, visit http://www.4GuysFromRolla.com/webtech/faq/Intermediate/faq3.shtml

Date Functions, Part 2


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)

SettingDescription
yyyyYear
qQuarter
mMonth
yDay of Year
dDay
wWeekday
wwWeek of Year
hHour
nMinute
sSecond

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:



Article Information
Article Title: Date Functions, Part 2
Article Author: Scott Mitchell
Article URL: http://www.4GuysFromRolla.com/webtech/faq/Intermediate/faq3.shtml


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