PDA

View Full Version : 1st, 2nd, 3rd etc date question


SteveH
02-22-2010, 04:32 PM
Hello

I am using the following for my ordinal date:

<%
'Declare variables
Dim curDayOftheWeek, curMonth, curDay
'Return the numeric day of the week Sunday = 1 etc
curDayOftheWeek=(weekday(date))
Select Case curDayOftheWeek
Case 1 Response.write("Sunday")
Case 2 Response.write("Monday")
Case 3 Response.write("Tuesday")
Case 4 Response.write("Wednesday")
Case 5 Response.write("Thursday")
Case 6 Response.write("Friday")
Case 7 Response.write("Saturday")
End select
'Write out the numeric day of the month
Response.Write(" "&day(date))
'Return the numeric day of the month and assign to variable curDay
curDay=day(date)
'Write out the ordinal suffix according to the numeric day of the day
Select Case curDay
Case 1,21,31 response.write("st")
Case 2,22 response.write("nd")
Case 3,23 response.write("rd")
Case else response.write("th")
End select
'add a space
response.write " "
'Return the numeric month of the year and assign to variable curMonth
'1=January, 2=February etc
curMonth=(month(date))
Select Case curMonth
Case 1 Response.write("January")
Case 2 Response.write("February")
Case 3 Response.write("March")
Case 4 Response.write("April")
Case 5 Response.write("May")
Case 6 Response.write("June")
Case 7 Response.write("July")
Case 8 Response.write("August")
Case 9 Response.write("September")
Case 10 Response.write("October")
Case 11 Response.write("November")
Case 12 Response.write("December")
End Select
%>


The code works, but the date looks like this: Monday 22nd February, whereas the 'st', 'nd', and 'rd' should be smaller than the anterior number.

How would I do that?

Thanks.

Steve

Fou-Lu
02-22-2010, 05:36 PM
You want a superscript for it yeah?
Unfortunately, I do not do ASP programming at all, so I'm not really familiar with it. Assumably, I can write html within the response.write, and if so you should be able to just do this:

Case 1,21,31 response.write("<sup>st</sup>")
Case 2,22 response.write("<sup>nd</sup>")
Case 3,23 response.write("<sup>rd</sup>")
Case else response.write("<sup>th</sup>")


Alternatively, you can use css to apply a class with a smaller font size and top vertical alignment.

You may need to wait around for old pedant to show up, he knows his asp much much much better than I know mine.


Wierd, I just noticed that asp doesn't have break in their switches?

Old Pedant
02-22-2010, 10:50 PM
The code also really really really needs a rewrite.

If all you want is "Monday 22nd February" then do it thus:

Select Case Day(Date())
Case 1,21,31: suffix = "st"
Case 2,22: suffix = "nd"
Case 3,23: suffix = "rd"
Case Else: suffix = "th"
End Select
Response.Write WeekdayName(Weekday(Date()) _
& " " & Day(Date()) & "<sup>" & suffix & "</sup> " _
& MonthName(Month(Date())


Yes, WeekdayName and MonthName have been built into VBScript since IIS version 3 and MSIE version 4. Say 1998, give or take a year.

Old Pedant
02-22-2010, 10:53 PM
Fou-lu: Yeah, no break in VBScript code or in VB or VB.NET. Also, technically you should have a colon after the case list unless the following statement is on the next line.

And also, technically, you should use parentheses around the value following Response.Write. (It works only because of course any expression is still an expression when enclose in parens. Wouldn't work if Response.Write took two arguments.)

But what is so surprising about no break? SQL doesn't use one in its CASE WHEN.

SteveH
02-23-2010, 10:21 AM
Sorry, Old Pendant, but I get the following error using that:

Expected ')'

/date_Test.asp, line 10

& MonthName(Month(Date())



It says it is expecting a ')', but adding one does not work, either.

Steve

Old Pedant
02-23-2010, 06:58 PM
Sorry...there are two missing right parens in that.

It's because I opted to use DATE() instead of just DATE. You *can* omit the parens from the call to DATE(), but it's poor practice and won't work in VB.NET, so you should keep them.

Anyway...

Select Case Day(Date())
Case 1,21,31: suffix = "st"
Case 2,22: suffix = "nd"
Case 3,23: suffix = "rd"
Case Else: suffix = "th"
End Select
Response.Write WeekdayName(Weekday(Date())) _
& " " & Day(Date()) & "<sup>" & suffix & "</sup> " _
& MonthName(Month(Date()))

SteveH
02-24-2010, 09:46 AM
Hello Old Pedant

It's great that a short amount of code works just as well!

The code you kindly posted is in a date.inc file and I have this:

<!--#include file="date.inc"-->

in all four pages of the site. The date script works - just what I wanted, thank you - except on one page (which I find quite odd) where I get this error:

Microsoft VBScript runtime error '800a01f4'

Variable is undefined: 'suffix'

/date.inc, line 6

Line 6 of the script you posted is this: Case Else: suffix = "th"

I don't know why it works fine on the other three pages, but not on this one.

Steve

Old Pedant
02-24-2010, 06:52 PM
Because that page has an
<% Option Explicit %>
line at the top an the others don't.

All that means is that the line
Dim suffix
is missing.

You can just add that line right before the Select Case and it should be fine on all pages.

SteveH
02-25-2010, 09:50 AM
Hello Old Pedant

Oh, I see!

So the other pages (which do not need Dim suffix) just ignore it?

Anyway, it works - thanks to you!

Cheers

Steve

Old Pedant
02-25-2010, 08:20 PM
Yes, if you don't code <% Option Explicit %> then VBScript will DIM all your variables for you. Expressly doing the DIM yourself is both good programming practice *and* saves a tiny bit of time when the code runs. Barely noticeabe, but what the heck, it all adds up.