|
This is part 4 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,
we have defined a value, displayed it on the page, as well as discussed some
date and time functions. We are now going to take that
exact same page and modify it. I am going to show you how to use If, Else, Then
statements with the date and time functions
you just learned.
Let's begin.
Part 4:
IF, ELSE, THEN
If, Else, Then can be used logically
to make the server perform certain tasks depending on what you have programmed.
The just jump right into an example.
Let's say you want to have a message
say good morning or good evening depending on the hour of the day. I'm using the
time since this was our last step. So we
are going to say If the time is
greater then 12 then say Good Evening
Else it must be before noon so say Good
Morning. We then close the If statement with
End If
|
<% If Hour(Now())
> 12 Then %>
Good Evening
<% Else %>
Good
Morning
<% End If
%>
| You can also have If statements inside of If Statements. So let's narrow it down to Good Morning, Good Afternoon, and Good evening.
<%
Dim intHour 'Variable to hold hour
intHour = Hour(Now())
If intHour > 12 Then'If hour is under 5 pm then afternoon
If intHour < 17 Then %>
Good Afternoon!
<% Else %>
Good Evening!
<% End If
Else %>
Good Morning!
<% End If %> |
If you used <> this means not equal to. So let's say if the hour is not equal to 12 then it's not between 12 and 1 p.m. so for humor's sake let's say that is your lunch time.
<%
Dim intHour 'Variable to hold hour
intHour = Hour(Now())
If intHour <> 12 Then %>It's not noon so it's not lunch time!
Else %>
It's 12 o'clock, lunch time!
<% End If %> |
This could also be inversely written and get the same result.
<%
Dim intHour 'Variable to hold hour
intHour = Hour(Now())
If intHour = 12 Then %>It's 12 o'clock, lunch time!
Else %>
It's not noon so it's not lunch time!
<% End If %> |
Add which ever codes you wish to your first asp page. I added
<%
Dim intHour 'Variable to hold hour
intHour = Hour(Now())
If intHour > 12 Then'If hour is under 5 pm then afternoon
If intHour < 17 Then %>
Good Afternoon!
<% Else %>
Good Evening!
<% End If
Else %>
Good Morning!
<% End If %> |
We will cover more If, Else, Then statement in following parts.
|
<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
<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> <%
Dim intHour 'Variable to hold hour
intHour = Hour(Now())
If intHour > 12 Then'If hour is under 5 pm then afternoon
If intHour < 17 Then %>
Good Afternoon!
<% Else %>
Good Evening!
<% End If
Else %>
Good Morning!
<% End If %> </font> </body>
</html> |
|