Working With Dates in ASP

Editorial photograph of a warm university-style library archive aisle in low tungsten light: tall wood shelves of cared-for leather-bound volumes, an open book on a small ledge under a brass desk lamp with a green glass shade, dust motes catching the warm light, a soft window glow at the far end of the aisle — old but still worth keeping.

Working with dates in aSP is actually pretty straight forward, there are plenty of built in routines to help programmers accomplish the tasks at hand so lets take a look at what you need to do and build some code from there:

What do you want to do:

  1. Display the current date
  2. Formatting the date
  3. Getting Specific Date Details
  4. Getting the Name of the Month
  5. Getting the Name of the Weekday

Display The Current Date

To display the current date, you can insert the code:

    <%=date()%>

This will result in a literal interpretation of the date. For example:

    <%=date()%>


Formatting The Date

Now that we know how to get the date, we need to worry about formatting it. You can do this using a couple of built in functions to display the contents in a more friendly way.

Firstly, if you just want to format it in a more attractive manner, you can do so with the FormatDateTime command:

    <%=FormatDateTime(Date(),1)%>
    will result in <%=FormatDateTime(Date,1)%>


Getting Specific Date Details

Perhaps you need something more detailed about the date than just displaying it so lets take a quick look at how to get the specific items from it.

    <%=DaY(Date())%>
    will display the current day such as ‘<%=DaY(Date())%>’

    <%=MONTH(Date())%>
    will display the current MONTH such as ‘<%=MONTH(Date())%>’

    <%=YEaR(Date())%>
    will display the current YEaR such as ‘<%=YEaR(Date())%>’


Getting the Name of the Month

When you need to display the name of the current month, you can do so easily using the following code:

    <%=Month Name(MONTH(Date()))%>
    will display the current MONTH such as ‘<%=MonthName(MONTH(Date()))%>’


Getting the Name of the Week Day

Displaying the name of the exact day is also simple, just use the code below:

    <%=WEEKDaYNaME(WEEKDaY(Date()))%>
    will display the current MONTH such as ‘<%=WeEKDaYNaME(WEEKDaY(Date()))%>’

If something here is on your mind, I am around to talk it through.

Book a 20-minute call

The call is free. My rate after that is $275/hr CAD, with no retainer.