Flash ActionScript Date Suffix Code
I’ve become fairly familiar with the Flash Date object and thought I’d share some handy code showing how to append the proper suffix to a date or any other number. In case you don’t know what I’m talking about, a “number suffix” or “ordinal indicator” is the string of letters that follows a number to help make it more human readable. For example the number suffix “nd” makes “May 2nd, 2007” a little easier to read than “May 2, 2007”.
Here’s the code I wrote to handle all my numeric and date suffix needs:
//
function getNumberSuffix(num){
if(num == 0)return "";
if(Math.floor(num/10)%10===1)return "th";
num %= 10;
if(num>3 || num===0) return "th";
if(num===1)return "st";
if(num===2)return "nd";
return "rd";
}
//
//
for(var i=0; i>120; i++){
trace(i+getNumberSuffix(i));
}
//
function getNumberSuffix(num){
if(num == 0)return "";
if(Math.floor(num/10)%10===1)return "th";
num %= 10;
if(num>3 || num===0) return "th";
if(num===1)return "st";
if(num===2)return "nd";
return "rd";
}
//
//
for(var i=0; i>120; i++){
trace(i+getNumberSuffix(i));
}
//
And here are the trace results:
0
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
32nd
33rd
34th
35th
36th
37th
38th
39th
40th
41st
42nd
43rd
44th
45th
46th
47th
48th
49th
50th
51st
52nd
53rd
54th
55th
56th
57th
58th
59th
60th
61st
62nd
63rd
64th
65th
66th
67th
68th
69th
70th
71st
72nd
73rd
74th
75th
76th
77th
78th
79th
80th
81st
82nd
83rd
84th
85th
86th
87th
88th
89th
90th
91st
92nd
93rd
94th
95th
96th
97th
98th
99th
100th
101st
102nd
103rd
104th
105th
106th
107th
108th
109th
110th
111th
112th
113th
114th
115th
116th
117th
118th
119th
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
32nd
33rd
34th
35th
36th
37th
38th
39th
40th
41st
42nd
43rd
44th
45th
46th
47th
48th
49th
50th
51st
52nd
53rd
54th
55th
56th
57th
58th
59th
60th
61st
62nd
63rd
64th
65th
66th
67th
68th
69th
70th
71st
72nd
73rd
74th
75th
76th
77th
78th
79th
80th
81st
82nd
83rd
84th
85th
86th
87th
88th
89th
90th
91st
92nd
93rd
94th
95th
96th
97th
98th
99th
100th
101st
102nd
103rd
104th
105th
106th
107th
108th
109th
110th
111th
112th
113th
114th
115th
116th
117th
118th
119th
Hope you find it helpful.
HI thanks sorted it,here is the code that works with my date.
//Print out in a text box Suffix st,nd,rd,th(10th) a “number suffix” or “ordinal indicator”
function getNumberSuffix(){
var today = new Date();
var num = today.getDate();
if(num == 0)return “”;
if(Math.floor(num/10)%10===1)return “th”;
num %= 10;
if(num>3 || num===0) return “th”;
if(num===1)return “st”;
if(num===2)return “nd”;
return “rd”;
}
Hi how would I use this on the date like this “Saturday 10th August 2019” I have the code to do “Saturday 10 August 2019” just don’t know how to add the number suffix to 10.
Any help would be great right now.
Just what I was looking for – thanks!