Computers & ProgrammingASPBackend Development

Formatting the Date in ASP

Working with Date and Time data is very common in any ASP web application. In this tutorial, we will cover how to properly use the various ASP Date related functions. In addition, we will take a closer look into the different formatting options that are available for you to use.

Built-in Functions

ASP provides a number of built-in date-related functions that can be used to find the current date and time. The three most common functions are date(), time(), and now().

Response.Write "Today's date is " & date() & "<br />"
Response.Write "The current time is " & time() & "<br />"
Response.Write "The date and time is " & now() & "<br />"

FormatDateTime Function

The FormatDateTime function can be used to format the results from the date-related function. You can pass an optional integer ranging from 0 through 4.

  • 0 – This is the default setting.
  • 1 – long date defined by the regional settings.
  • 2 – short date defined by the regional settings.
  • 3 – time format defined by the regional settings.
  • 4 – time format using military time HH:MM.
Response.Write(FormatDateTime(Date,0) & "<br />")
Response.Write(FormatDateTime(Date,1) & "<br />")
Response.Write(FormatDateTime(Date,2) & "<br />")
Response.Write(FormatDateTime(Now,3) & "<br />")
Response.Write(FormatDateTime(Now,4) & "<br />")

Date and Time Functions

ASP (VBScript) also provides a number of date- and time-related functions that are very useful. The DatePart function is also very useful. It allows you to retrieve a specific part of a date/time value.

Year(date)Returns the year portion from date.<% Response.Write(Year(date)) %>
Month(date)Returns the month portion from the date.<% Response.Write(Month(date)) %>
MonthName(Month#)Returns a string. Optional true/false for name or abbr.<% Response.Write(MonthName(12, true)) %>
Day(date)Returns the day portion from the date.<% Response.Write(Day(date)) %>
Hour(time)Returns the hour portion from time.<% Response.Write(Hour(time)) %>
Minute(time)Returns the minute portion from time.<% Response.Write(Minute(time)) %>
Second(time)Returns the second portion from time.<% Response.Write(Second(time)) %>
DatePart("yyyy", date)Returns the current year.<% Response.Write(DatePart("yyyy", date)) %>
DatePart("q", date)Returns the current quarter.<% Response.Write(DatePart("q", date)) %>
DatePart("m", date)Returns the current month.<% Response.Write(DatePart("m", date)) %>
DatePart("ww", date)Returns the week # of the year.<% Response.Write(DatePart("ww", date)) %>
DatePart("h", time)Returns the current hour.<% Response.Write(DatePart("h", time)) %>
DatePart("n", time)Returns the current minute.<% Response.Write(DatePart("n", time)) %>
DatePart("s", time)Returns the current second.<% Response.Write(DatePart("s", time)) %>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top