469,352 Members | 2,119 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Ordinal (st, nd, rd, th) dates in javascript

Hello everyone.

My first post... I'd like to add an automatically updating date field
to a webpage. I found the below example on the internet which works
brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
instead of a cardinal date (e.g. 1, 2, 3, 4).

How could I do that with this small bit of javascript code?

<script language = "JavaScript">
var now = new Date();
var monNames = new
Array("January","February","March","April","May"," June","July","August","September","October","Novem ber","December");
document.write("Last updated: " + now.getDate() + " " +
monNames[now.getMonth()] + " " + now.getFullYear());
</script>

Thanks in advance for any advice and stuff.

Jul 23 '05 #1
12 11297
Ivo
"Joseph Numpty" wrote
My first post... I'd like to add an automatically updating date field
to a webpage. I found the below example on the internet which works
brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
instead of a cardinal date (e.g. 1, 2, 3, 4).

How could I do that with this small bit of javascript code?

<script language = "JavaScript">
Drop the language. Use type instead. Like so:
<script type="text/javascript">
var now = new Date();
That is the current date, not the last modified date.
var monNames = new
Array("January","February","March","April","May"," June","July","August","Sep
tember","October","November","December"); document.write("Last updated: " + now.getDate() + " " +
Make that:
document.write("Last updated: " + ordi(now.getDate()) + etc.

and have a function like so:
function ordi(n){
var s='th';
if(n===1) s='st';
if(n===2) s='nd';
if(n===3) s='rd';
return n+s;
}
monNames[now.getMonth()] + " " + now.getFullYear());
</script>


For example.
--Iv
Jul 23 '05 #2
>On Thu, 9 Sep 2004 11:23:31 +0200, "Ivo" <no@thank.you> wrote:
<snip>
For example.
--Iv


Thanks very much for your help. You've solved my problem brilliantly.

Joe
Jul 23 '05 #3
On 9/9/04 10:23 am, Ivo wrote:
and have a function like so:
function ordi(n){
var s='th';
if(n===1) s='st';
if(n===2) s='nd';
if(n===3) s='rd';
return n+s;
}


This would be better:

function ordi(n){
var s='th';
if(n===1 || n==21 || n==31) s='st';
if(n===2 || n==22) s='nd';
if(n===3 || n==23) s='rd';
return n+s;
}

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #4
>On Thu, 09 Sep 2004 10:50:36 +0100, Philip Ronan <ph***********@virgin.net> wrote:

This would be better:

function ordi(n){
var s='th';
if(n===1 || n==21 || n==31) s='st';
if(n===2 || n==22) s='nd';
if(n===3 || n==23) s='rd';
return n+s;
}


Thanks for that Philip. I added in the early 20's but forgot 31. D'oh!

Joe
Jul 23 '05 #5

"Ivo" wrote

.............
and have a function like so:
function ordi(n){
var s='th';
if(n===1) s='st';
if(n===2) s='nd';
if(n===3) s='rd';
return n+s;
}

......................
or

function ordi(n){
return n+(arguments.callee.map[n]||"th");
}
ordi.map={1:'st',21:'st',31:'st',2:'nd',22:'nd',3: 'rd',23:'rd'};

avoiding all the if's

bye

m
Jul 23 '05 #6
Philip Ronan wrote:
On 9/9/04 10:23 am, Ivo wrote:
and have a function like so:
function ordi(n){
var s='th';
if(n===1) s='st';
if(n===2) s='nd';
if(n===3) s='rd';
return n+s;
}


This would be better:

function ordi(n){
var s='th';
if(n===1 || n==21 || n==31) s='st';
if(n===2 || n==22) s='nd';
if(n===3 || n==23) s='rd';
return n+s;
}


or:-

function ordi(n){
var s;
switch(n%10){
case 1:
s = 'st';
break;
case 2:
s = 'nd';
break;
case 3:
s = 'rd';
break;
default:
s = 'th';
break;
}
return n+s;
}

Richard.
Jul 23 '05 #7
Richard Cornford skrev:
or:-

function ordi(n){
var s;
switch(n%10){
case 1:
s = 'st';
break;
case 2:
s = 'nd';
break;
case 3:
s = 'rd';
break;
default:
s = 'th';
break;
}
return n+s;
}

Richard.


This is written a few days before the 11st September.

--
Henrik
Jul 23 '05 #8
Richard Cornford wrote:
Philip Ronan wrote:
<snip> function ordi(n){
var s;
switch(n%10){
case 1:
s = 'st';
break;
What about "11"?
case 2:
s = 'nd';
break;
What about "12"?
case 3:
s = 'rd';
break;
What about "13"?
default:
s = 'th';
break;
}
return n+s;
}

Richard.

Mick
Jul 23 '05 #9
Fox


Joseph Numpty wrote:
Hello everyone.

My first post... I'd like to add an automatically updating date field
to a webpage. I found the below example on the internet which works
brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
instead of a cardinal date (e.g. 1, 2, 3, 4).

How could I do that with this small bit of javascript code?

<script language = "JavaScript">
var now = new Date();
var monNames = new
Array("January","February","March","April","May"," June","July","August","September","October","Novem ber","December");
document.write("Last updated: " + now.getDate() + " " +
monNames[now.getMonth()] + " " + now.getFullYear());
</script>

Thanks in advance for any advice and stuff.


handles 0th, and numbers >= 100 (e.g.: 101st) -- in other words, a
general purpose method.

Number.prototype.toOrdinal = function()
{
var n = this % 100;
var suff = ["th", "st", "nd", "rd", "th"]; // suff for suffix
var ord= n<21?(n<4 ? suff[n]:suff[0]): (n%10>4 ? suff[0] : suff[n%10]);
return this + ord;
}

use:

num.toOrdinal();

examples:

(111).toOrdinal() => 111th
(2004).toOrdinal() + " year of the Christian era"...
etc...

does not test for negatives (but programmers are careful about these
things, right?)
Jul 23 '05 #10
JRS: In article <ps********************************@4ax.com>, dated
Thu, 9 Sep 2004 10:05:18, seen in news:comp.lang.javascript, Joseph
Numpty <jo********@tiscali.co.uk> posted :
My first post... I'd like to add an automatically updating date field
to a webpage. I found the below example on the internet which works
brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
instead of a cardinal date (e.g. 1, 2, 3, 4).

How could I do that with this small bit of javascript code?

<script language = "JavaScript">
var now = new Date();
var monNames = new
Array("January","February","March","April","May", "June","July","August","Septemb
er","October","November","December");
document.write("Last updated: " + now.getDate() + " " +
monNames[now.getMonth()] + " " + now.getFullYear());
</script>

Thanks in advance for any advice and stuff.


Read the FAQ, and, with its aid, find
<URL:http://www.merlyn.demon.co.uk/js-demos.htm#Suff>
for a variety of methods.

There is a link in <URL:http://www.merlyn.demon.co.uk/js-date9.htm>.
You should realise that :-
A. Most users already know the date
B. And/or don't care
C. And can easily find out independently of their computer
D. But their computer is very probably already showing it
E. You show the computer's date, which may be wrong.
F. You show the current date, not the last modified date
G. Your method does not update the displayed page at midnight
H. For lastModified, see my js-date2.htm#lM

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11
rh
"mehdi amini" wrote:
.....................
or

function ordi(n){
return n+(arguments.callee.map[n]||"th");
}
ordi.map={1:'st',21:'st',31:'st',2:'nd',22:'nd',3: 'rd',23:'rd'};

avoiding all the if's


or, by going a bit beyond the OP's request and creating a general utility:

function ord(n) {
var sfx = ["th","st","nd","rd"];
var val = n%100;
return n + (sfx[(val-20)%10] || sfx[val] || sfx[0]);
}
../rh
Jul 23 '05 #12
Mick White wrote:
Richard Cornford wrote:

<snip>
function ordi(n){
var s;
switch(n%10){
case 1:
s = 'st';
break;


What about "11"?

<snip>

Yes, that is not good at all. I should have just stuck with suggestion
changing the successive - if - statements to - if/else - as they would
have been mutually exclusive.

Richard.
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Colin Steadman | last post: by
4 posts views Thread by Richard Hollenbeck | last post: by
7 posts views Thread by Bambero | last post: by
2 posts views Thread by stewartfip | last post: by
22 posts views Thread by mike | last post: by
4 posts views Thread by Mark Tarver | last post: by
7 posts views Thread by Dylan Parry | last post: by
9 posts views Thread by David Jackson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.