PDA

View Full Version : Tutorial: Getting the "st, nd, rd, th" on dates!


Mhtml
03-28-2003, 03:46 PM
[EDIT: Changed it to reflect what Whammy pointed out!]
Well I was pondering over this for a few minutes as I was making my "website in a box" instant ecommerce website.

How can you get the little extension thingies on the end of your date? IE - 1st, 2nd, 3rd, 4th ...

The answer is really quite simple, I thought someone may find some use of it!


Function dayExt (theDay)
If theDay = 11 Or 12 Or 13 Then
strExt = "th"
Else
Select Case Right(theDay,1)
Case 1
strExt = "st"
Case 2
strExt = "nd"
Case 3
strExt = "rd"
Case Else
strExt = "th"
End Select
End If
dayExt = strExt
End Function


How simple is that? Just pass a day number to the function and it'll return the proper extension thingy.

dominicall
03-28-2003, 04:02 PM
Nice - like it - will steal... I mean use... it

dominicall :D

Mhtml
03-28-2003, 04:04 PM
Yay! Someone found it useful.. hehe..

whammy
03-28-2003, 11:44 PM
Not bad. There is a slight problem with it though.

11st
12nd
13rd

;)

P.S. Although, I can probably use part of what you wrote to improve the very first ASP script I ever wrote (below):

<%
Dim strDateExt, d, strEnglishDate
strDateExt = "th"
d = Date()
If Day(d) = 1 OR Day(d) = 21 OR Day(d) = 31 Then
strDateExt = "st"
ElseIf Day(d) = 2 OR Day(d) = 22 Then
strDateExt = "nd"
ElseIf Day(d) = 3 OR Day(d) = 23 Then
strDateExt = "rd"
End If
strEnglishDate = WeekDayName(WeekDay(d)) & ", " & _
MonthName(Month(d)) & " " & _
Day(d) & strDateExt & ", " & _
Year(d)
%>

whammy
03-28-2003, 11:52 PM
<%
Function dayExt(ByVal theDay)
If IsNumeric(theDay) Then
If CInt(theDay) <> theDay Then Exit Function
If (theDay < 11) OR (theDay > 13) Then
Select Case Right(theDay,1)
Case 1
dayExt = "st"
Case 2
dayExt = "nd"
Case 3
dayExt = "rd"
Case Else
dayExt = "th"
End Select
Else
dayExt = "th"
End If
End If
End Function

Dim i
For i = 1 to 31
Response.Write(i & dayExt(i) & "<br />" & vbCrLf)
Next
%>

<% = dayExt("R") %>


That works... and it also uses ByVal (since the default ByRef would alter the variable you passed as a parameter, which would very likely produce unexpected results if you attempted to use the variable later!).

I also put in some checks to make sure that the right type of input was received.

P.S. I took out the strExt variable, since it was unnecessary in this case - that way, as soon as something matches, BAM, you exit the function. :)

P.P.S. Not disparaging your idea, though - it was very good and creative! And it definitely improved on my original "date extension script" with a little tweaking. :)

I just write a lot of functions at work every week, and I have to rigorously test them, so I have a pretty good idea of what to watch out for (especially if other developers will be using them - and I know where my functions have failed before!).

Not to mention you wouldn't believe what I've seen people break - and I'm constantly learning new ways to break applications, you should see the faces some people make when I point out some potentially bad security flaws (and I'm fairly sure I don't know 75% of them, at a generous estimate - but I might be doing better than that!). ;)

Mhtml
03-29-2003, 09:48 AM
Damn 11/12.. Thanks for pointing that out Whammy!

dominicall
03-29-2003, 09:51 AM
LOL - I never noticed it either....

Nice function though

scroots
03-29-2003, 10:57 AM
i can't code but have the rules bit.
IF one digit long and value is 1 write digit+st
IF one digit long and value is 2 write digit+nd
IF one digit long and value is 3 write digit+rd
IF two digits long and value is 21 write digits+st
IF two digits long and value is 22 write digits+nd
IF two digits long and value is 23 write digits+rd
ELSE write digits+th

that would be a basic process of who you could do it.

scroots

Mhtml
03-29-2003, 11:01 AM
That wouldn't work.

There are 31 max days.
All the suffixes (I figured out the word!) will be determined by the last number with the exception of 11, 12 and 13 ...

[edit:]In algothithmic form -


If number is 11 or 12 or 13 then
suffix = th
Else
if last number is 1 then
suffix = st
else If last number is 2 then
suffix = nd
else If last number is 3 then
suffix = rd
else
suffix = th
end if
End If

Morgoth
03-29-2003, 10:15 PM
How interesting...
Too bad I can't use it... :o

Mhtml
03-29-2003, 10:17 PM
Y not?

Morgoth
03-30-2003, 01:50 AM
i DON'T HAVE DATES THAT MATTER THAT MUCH TO ME

Morgoth
03-30-2003, 01:51 AM
Gotta love caps lock...

Mhtml
03-30-2003, 01:52 AM
lol..

whammy
03-30-2003, 01:15 PM
Well, that's exactly the same logic I used.

If it's less than 11 or greater than 13, apply the right() thing. Otherwise, the date extension is still "th". ;)

Also notice the check I put in place, which doesn't throw an error (doesn't do anything, actually) if someone uses the latter part of this (not to mention the fact that I tested to make sure it works!):

<%
Dim i
For i = 1 to 31
Response.Write(i & dayExt(i) & "<br />" & vbCrLf)
Next
%>

<% = dayExt("R") %>