2:25 pm Saturday, July 05, 2008


  Navigation
  Help?
Contact Aaron for all inquiries.
 

  News

ASP Inline Calendar 3.9 Beta 1
ASP Games
ASP Inline Corporate Calendar 3.8 BETA 1 Demo
Calendar Updates
 

A Better World By Design
 
  Creating Your First ASP Page Tutorial
AaronOutpost : ASP / Tutorials / ASP 101 / Date & Time Functions

ASP 101: Date & Time Functions
By: Aaron B. Copyright AaronOutpost.com

 

    This is part 3 of 6 of ASP 101 by AaronOutpost.com. If you are unsure of what we may be talking about in the tutorial, view the previous parts.

So far we have created your first ASP page and we have defined a value and displayed it on the page. We are now going to take that exact same page and modify it. I am going to show you how to mix ASP with HTML.

Let's begin.

 

Part 3:
Date & Time

START YOUR PAGES
Open your favorite text or HTML editor. It can be FrontPage, NotePad, FireWorks, Arachnophilia, etc... It don't matter.

Open first_asp_page_part1.asp, the file we modified in part 2. Your code should look like this.

<html>

<head>

<title>My First ASP Page</title>

</head>

<body bgcolor="#FFFFFF" text="#000000" link="#003300" vlink="#006600" alink="#689577">

 

<%

'Dimension variables

Dim MyVariable 'Comment on what this variable holds

 

'Define a value for the MyVariable
MyVariable = "I am learning ASP! This is my first programmed variable!"
%>

<br><br>
<font face="Arial" size="2" color="#000000">
<%
= MyVariable %></font>

</body> </html>

 

 

Date & Time Functions

The most popular functions are Now, Time, and Date().

The Now function includes Date & Time.
There is also the Time() function which only includes the time.
Date() is the date only in your format. US MM/DD/YYYY

So using what we learned in Mix ASP with HTML let's display the Now and Time Functions

Server Date & Time is: <%= Now() %>

Server Time is: <%= Time() %>

Server Date is: <%= Date() %>

 

You can also strip the hour using the hour function and same for Minute and Second.

Current Hour is: <%= Hour(Now()) %>

Current Minute is: <%= Minute(Now()) %>

Current Second is: <%= Second(Now()) %>

 

This can similarly be done with other portions. I am sure you can figure out each of the ones below.

Integer for this month: <%= Month(Date()) %>
Month name for this month is: <%= MonthName(Month(Date())) %>
Integer for this year: <%= Year(Date()) %>
Day is: <%= Day(Date()) %>

You can also format the date and time listed using the DateFormat function. You can use Date, Time, or now function inside this function where you see Date.

FormatDateTime(Date, 4)

The following table has been exerted from http://msdn.microsoft.com which has many more descriptions of built in functions.

ConstantValueDescription
vbGeneralDate0Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
vbLongDate1Display a date using the long date format specified in your computer's regional settings.
vbShortDate2Display a date using the short date format specified in your computer's regional settings.
vbLongTime3Display a time using the time format specified in your computer's regional settings.
vbShortTime4Display a time using the 24-hour format (hh:mm).

 

If you are going to be storing a date or time into a database that could have been obtained by a user submission you need to use CDate Function will will convert it to data type. This was used in the making of my calendar.

<%

'Define date.
MyDate = "January 15, 1982"

'Convert to Date data type. 
MyShortDate =
CDate(MyDate)

'Define time.
MyTime = "4:35:47 PM"

'Convert to Date data type
MyShortTime =
CDate(MyTime)

%>

So now let's take what we learned and add this to our page.

<html>

<head>

<title>My First ASP Page</title>

</head>

<body bgcolor="#FFFFFF" text="#000000" link="#003300" vlink="#006600" alink="#689577">

 

<%

'Dimension variables

Dim MyVariable 'Comment on what this variable holds

 

'Define a value for the MyVariable
MyVariable = "I am learning ASP! This is my first programmed variable!"
%>

<br><br>
<font face="Arial" size="2" color="#000000">
<%
= MyVariable %><br><br>

Server Date & Time is: <%= Now() %><br><br>

Server Time is: <%= Time() %> <br><br>

Server Date is: <%= Date() %><br><br>

Current Hour is: <%= Hour(Now()) %><br><br>

Current Minute is: <%= Minute(Now()) %><br><br>

Current Second is: <%= Second(Now()) %><br><br>

vbGeneralDate: <%= FormatDateTime(Now(), 0) %><br><br>

vbLongDate: <%= FormatDateTime(Now(), 1) %><br><br>

vbShortDate: <%= FormatDateTime(Now(), 2) %><br><br>

vbLongTime: <%= FormatDateTime(Now(), 3) %><br><br>

vbShortTime: <%= FormatDateTime(Now(), 4) %><br><br>

</font> </body> </html>

CONGRATULATIONS!!!

Now, you have completed part 3 of 6 Learning ASP 101 by AaronOutpost.com.

Open Viewable Example
Continue to part 4: If, Else, Then Statement


 


AaronOutpost : ASP / Tutorials / ASP 101 / Date & Time Functions



Aaron Outpost West Virginia, USA A Mark of _-Squire-_ Creations Inc.  CopyrightŠ 2005 All Rights Reserved