Tuesday, October 31, 2006

Using ASP to put the Current Date on a Page

Ever wanted to put the current date and or time on a page? ASP makes it easy using two built in functions:

Date() (or Now())
FormatDateTime()

Date() returns the current date
Now() returns the current date and time

You can use either one with FormatDateTime().

FormatDateTime allows you to change the way the date/time appears.

FormatDateTime(date(),vbgeneraldate) = 10/31/2006
FormatDateTime(date(),vblongdate) = Tuesday, October 31, 2006
FormatDateTime(date(),vbshortdate) = 10/31/2006
FormatDateTime(now(),vblongtime) = 10:25:49 AM
FormatDateTime(now(),vbshorttime) = 10:25

You can use number values instread of the vb values, per the following chart:
ConstantValueDescription
vbGeneralDate0Display a date in format mm/dd/yy. If the date parameter is Now(), it will also return the time, after the date
vbLongDate1Display a date using the long date format: weekday, month day, year
vbShortDate2Display a date using the short date format: like the default (mm/dd/yy)
vbLongTime3Display a time using the time format: hh:mm:ss PM/AM
vbShortTime4Display a time using the 24-hour format: hh:mm

No comments: