ASP Training - The Basics: Part 2
Date & Weekday
So onto the next part of the introductory script. There is the line currentDate = Date. Date is an ASP function that gives you the current date. Warning : remember that this script is being run by the web server and NOT the browser, so date will be the current date of the web server, not the date of whoever is looking at your script; important when thinking of the web as a global system where browsers cross datelines.
date returns the current date so currentDate = Date sets the variable currentDate to todays date. Simple.
The next part is also simple.weekDayValue = weekday(currentDate). We have todays date from currentDate = Date, we now feed this information to the ASP function weekday. Now weekday needs a date to work on, and we give it a date to work on by feeding it the variable we just created, and we do this by putting it between the two brackets.
Giving a date weekday will return a number between 1 and 7 (where 1 is equivalent to a Sunday, 2 is Monday all the way to 7 which is Saturday). Which is why we again use a variable to hold the returned information (in this case weekDayValue).
What we have now ended up with is a variable weekDayValue, that holds the current day of the week as a value ranging from 1 to 7. We need to convert that into a meaningful name, e.g. instead of "1" we want "Sunday". To do that we use the if..then...else...end if statement that makes up the majority of the script.
If...then...elseif...else...end if
The main section of the script is concerned with changing a number into a word. The if...then...else strucute should be fairly straight forward.
if weekDayValue = 1 then
weekDayActual = "Sunday"
elseif weekDayValue = 2 then
weekDayActual = "Monday"
elseif weekDayValue = 3 then
weekDayActual = "Tuesday"
elseif weekDayValue = 4 then
weekDayActual = "Wednesday"
elseif weekDayValue = 5 then
weekDayActual = "Thursday"
elseif weekDayValue = 6 then
weekDayActual = "Friday"
elseif weekDayValue = 7 then
weekDayActual = "Saturday"
else
weekDayActual = "Ooops, something's not right here!"
end if
All this structue does is test the value we got from the weekday function (we we put in the variable weekDayValue. So the first if checks to see if weekDayValue is 1. If it is then a new variable (weekDayActual) is set with actual name of the day.
If the weekDayValue is not 1 then the script skips to the next line which is elseif, this checks to see if weekDayValue is 2. If it is then weekDayActual is set to "Monday".
This is then carried on through 3, 4, 5, 6, and 7. The final check is the else. This is a catch all check, if none of the previous checks have worked the final else is applied; in this case we would set an error message, because we havn't been able to match a weekDayValue to a proper name, which certainly means that something has gone wrong.
All if structures are finished off with end if.
You can make your ifs as simple or as complex as you like. You can have an if that just does a single check or you can have ifs inside other ifs (called nesting).