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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date st, nd, rd, th issue 1

Status
Not open for further replies.

EvertonFC

Technical User
Nov 24, 2004
107
GB
Hello

I have the following script on one .asp page:

<HEAD>
<%
Response.Write "<TABLE ALIGN=""TOP""><TR><TD><b><FONT FACE=VERDANA COLOR=#FFFFFF SIZE=1>"

Response.Write WriteDate (Date)

Function Suffix(number)
SELECT CASE Right(Number,1)
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 "</FONT></b></TD></TR></TABLE>"
%>
</HEAD>

It seems to work when the date is 1st, 2nd, 3rd, 4th, but when it comes to day 12 of the month or day 13, (or day 22 or 23, for that matter), the 1st, 2nd, 3rd does not work (it uses 'th' for all these).

Is there a way around this?

Many thanks.

EvertonFC
 
How about an if statement around the select like this - it seems to work for me!

Function Suffix(Number)
if ((len(Number)=1) OR (left(Number,1)<>"1")) then
SELECT CASE Right(Number,1)
CASE 1
temp = "st"
CASE 2
temp = "nd"
case 3
temp = "rd"
CASE ELSE
temp = "th"
END SELECT
else
temp = "th"
end if
Suffix = number & temp
End Function

A computer always does what you tell it to, but rarely does what you want it to.....
 
Take a look at this thread in the Javascript forum: thread216-1155767
The code is Javascript (most of it but the whole thread get's funky) but you can see the logic used.

This was one of our blowing off work and having fun threads. :)

Paranoid? ME?? WHO WANTS TO KNOW????
 
why not simply use multiple values in the case statement?
because I've never come across the 11st or 13rd of the month [lol]


Code:
Function Suffix(DayNumber) 
SELECT CASE DayNumber
CASE 1,21,31
temp = "st" 
CASE 2,22
temp = "nd" 
case 3 
temp = "rd" 
CASE ELSE 
temp = "th" 
END SELECT 
Suffix = DayNumber & temp 
End Function


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Chris, when your function runs with a number of 111 it would come out 111th.
The trick is recognizing if there is a second digit and if it is a 1 then you set to th. Any other digit in the second position would return according to the rest of the case.
This is not the prettiest code but it works.
Code:
mynumber = "101"

endnum = Right(mynumber,1)
If Len(mynumber) >1 Then
  If Mid(mynumber,Len(mynumber)-1,1) = 1 Then
    endnum = "4"
  End If
End If

SELECT CASE endnum
CASE 1 
endtag = "st" 
CASE 2 
endtag = "nd" 
case 3 
endtag = "rd" 
CASE ELSE 
endtag = "th" 
END SELECT 
Suffix = mynumber & endtag
Response.Write Suffix

Paranoid? ME?? WHO WANTS TO KNOW????
 
when your function runs with a number of 111 it would come out 111th.
Yes it would,
I did take it that it would be safe not to add in a check for DayNumber < 1 or DayNumber > 31 and given that the OP is using
Code:
Suffix(Day(theDate))
it seemed fairly safe to assume the the function would be used in conjuction with another date function.

Code:
Function Suffix(DayNumber)
if DayNumber < 1 or DayNumber > 31 then
Suffix = "Day out of range"
else
SELECT CASE DayNumber
CASE 1,21,31
temp = "st" 
CASE 2,22
temp = "nd" 
case 3 
temp = "rd" 
CASE ELSE 
temp = "th" 
END SELECT 
Suffix = DayNumber & temp 
end if
End Function


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
You are right, it depends on what he intends to display and if it is just a day of the month value it would be fine.
But if he was displaying the day of the year such as 141st day of 2005, then the alternative is needed.

All I did in my test was to identify if the date was greater than 1 digit and the second digit was a 1 then it would end up with a th on the end, any other numbers would evaluate with the last digit.

We had fun with this in the Javascript forum and got the code down to a single line for the whole function. Also went to great lengths to make it as unreadable as possible without actually obfuscating it. :)


Paranoid? ME?? WHO WANTS TO KNOW????
 
theniteowl:
Here's my addition to that thread :)
Code:
Function DoIt(x)
	DoIt = Array("th","st","nd","rd")(Round(((x + 6) mod 10)/13) * (x mod 10) * (Round((x mod 100)/12 - .4,0) * Fix(((x + 85) mod 100)/95) * -1 + 1))
End Function

barcode_1.gif
 
That's good.
One line and a brain buster to try and decipher.
One of those functions you MUST have a comment for or you will not remember what they hell it is supposed to do. :)


Paranoid? ME?? WHO WANTS TO KNOW????
 
That one deserves a star for no better reason than it is so wonderfully compact and convoluted to the point of artistic.


Stamp out, eliminate and abolish redundancy!
 
Sorry for the delay ingetting back to this thread.

Many thanks to all those who did their best to assit me.

This is what works (at least, yesterday, it said 22nd December and not 22th December!):

Function Suffix(DayNumber)
SELECT CASE DayNumber
CASE 1,21,31
temp = "st"
CASE 2,22
temp = "nd"
case 3
temp = "rd"
CASE ELSE
temp = "th"
END SELECT
Suffix = DayNumber & temp
End Function

Many thanks, Chris.

EvertonFC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top