Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

23rd March showing as 23th March

Status
Not open for further replies.

SteveHigh

Technical User
Jan 17, 2007
158
GB
Hello

I have the following script which I use for the date on my online page:

<%
Response.Write
Response.Write WriteDate (Date)

Function Suffix(number)
SELECT CASE Number
CASE 1
temp = "st"
CASE 2
temp = "nd"
case 3
temp = "rd"
CASE ELSE
temp = "th"
END SELECT
Suffix = number & temp
End Function

Function WriteDate(theDate)
WriteDate = Suffix(Day(theDate)) & " " & Monthname(Month(theDate)) & " " & Year(theDate)
End Function
Response.Write
%>

I have noticed today, though, that depite the 'rd' in the code above, the date is showing as 23th.

Yet I am unsure about what might be wrong.

Any advice or suggestions would be welcome.

Thanks.

Steve
 
You have a select case on the day number. Since the day number is 23, (and not 3), the ELSE part takes affect. My suggestion is that you MOD the number with 10 and use that for the case. Like this...

Code:
Function Suffix(number) 
SELECT CASE Number [!]mod 10[/!]
CASE 1 
temp = "st" 
CASE 2 
temp = "nd" 
case 3 
temp = "rd" 
CASE ELSE 
temp = "th" 
END SELECT 
Suffix = number & temp 
End Function

If you don't know what [!]mod[/!] does, I suggest you do a little research on it.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Unfortunately, my suggestion breaks for numbers like, 11, 12, and 13. They show as 11st, 12nd, and 13rd. So, while my suggestion fixes the 20's, it breaks the teens.

So, forget the mod stuff (you should still learn about it though). Instead, I recomend you change your function to...

Code:
Function Suffix(number) 
	
    SELECT CASE Number
        CASE 1, 21, 31 
            temp = "st" 
        CASE 2, 22 
            temp = "nd" 
        CASE 3, 23 
            temp = "rd" 
        CASE ELSE 
            temp = "th" 
    END SELECT 

    Suffix = number & temp 
	
End Function

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I had to do this once and it seemed more robust to create a class consisting of an array (30) and assigned the appropriate ending to each element. Then you simply grab the array element that = datepart(d,now())



[sub]____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done[/sub]
 
That should be datepart(d,now())-1

[sub]____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done[/sub]
 
Something like
Code:
<%
Dim endDay : endDay = Array("st", "nd", "rd", "th", "th", _
							"th", "th", "th", "th", "th", _
							"th", "th", "th", "th", "th", _
							"th", "th", "th", "th", "th", _
							"st", "nd","rd", "th", "th", "th", _
							"th", "th", "th", "th","st")

Response.Write DatePart("d",Now()) & endDay(DatePart("d",Now()-1))

%>

I didn't really double check all of those that great but I think they match correctly


[sub]____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done[/sub]
 
Hello

Many thanks for both very useful posts.

Both seem to make sense. I'll try them and post back.

Cheers

Steve
 
Code:
function suffix(n)
	select case (n)
		case 12,13,14,15,16,17,18,19
			suffix = n&"th"
		case else
			select case right(cstr(n),1)
				case "1"
					suffix = n&"st"
				case "2"
					suffix = n&"nd"
				case "3"
					suffix = n&"rd"
				case else
					suffix = n&"th"
			end select
	end select
end function

}...the bane of my life!
 
Have a look at Tarwn's solution on thread333-1167544. It is absolutely brilliant. Almost as good as Zeller's congruence for dates.
 
Yikes, I had forgotten about that one even though I have been following this thread. Don't ask me how it works without a bit more coffee in my system :) (and that would be why I don't use functions like that in production code)

In this case I would go with one of the Case statements for future readability and efficiency. However I would try to compact it more, maybe using the Select Case True hack I wrote about a while back:
Code:
Function Suffixify(num)
   Select Case True
      Case num >= 11 And num <= 13
         Suffixify = "th"
      Case Right(num,1) >= 4
         Suffixify = "th"
      Case Else
         Suffixify = Array("th","st","nd","rd")(Right(num,1)+1)
   End Select
End Function

Ok, so one silly part that probably should just be broken into several cases :)

Basically I take out the 11-13 range first as the major exception to the general rules. Then I go ahead and grab >=4 as the most commonly occurring to get out of the statement as fast as possible. The last stage is to cover whats left (0-3) and I use the array to grab those values quickly rather than put in 3-4 more cases and conditional checks.
The main reason I did it in this order is for efficiency. With the 4-9 set in the second case 50% of the numbers that are run through this Select Case will only have to run through two conditionals, instead of running through all of the special cases before getting to an Else. The last set combines an array and right() as a single statement instead of using 3-4 more conditional Right() statements.

Just my stab at a readable version :)

Now another short version:
Code:
Function Suffixify2(num)
   Suffixify2 = Array("th","st","nd","rd")((((num >= 11 And num <= 13) Or Right(num,1) >= 4) + 1) * Right(num,1))
End Function
This one uses boolean logic to determine if the value should be a "th". Basically the whole first section is all of the cases where the number should be "th" (except the 0 case). This will return True (-1) or False (0). Add 1 and suddenly all of our "th"s are 0's and our non-"th"s are 1. Multiply that value against the right-most digit and we either end up with 0 still (th) or 1-3. It then grabs the value from the array. This should work for everything two digits and positive. You could easily expand the function by using a Right(num,2) in the first pair of checks for 11-13.

Whew, I need more coffee :p

-T

 
I just noticed that +1 should be there in my first example in the Case Else...oops, thats what you get for writing code on the fly during the first cup of coffee...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top