Effectively format dates and times with ASP

script written by

Description:

Formatting the date and time into a specific format can be important, especially for posting to a database and for consistency. VBScript has a built in function called "FormatDateTime" which combined with a value for the type of date or time, can "prepare" the field for accuracy before it gets put into a database field.

There are 5 constants with associated values that can be used. 0 is vbGeneralDate, 1 is vbLongDate, 2 is vbShortDate (00/00/00), 3 is vbLongTime and 4 is vbShortTime. 1, 2 and 3 are dependent on your local machines locale settings (or regional settings) which are set in your machines control panel under "Regional Settings". As shown below, the numbers are defined after the date/time or a variable containing a date time, separated with a comma.

'Formatting date and time with FormatDateTime

dim strDate
dim strFormTime

strFormTime = Request.Form("time")

strDate = FormatDateTime(12-apr-02, 2)

'or via form input

strFormTime = FormatDateTime(strFormTime, 3)



Remember, if you get stuck or need to ask any questions, register with the forums and tell us about it!

Partners Partners