When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
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:


FAQ Table of Contents   Date Functions Passing Variables Between ASP Pages 


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article