473,372 Members | 823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 11724
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
5
by: SimonC | last post by:
Help needed for a Javascript beginner. As above in the subject... i need a javascript to run this, but not in the form of a web-page. I want to calculate it between 2 fields in a database that...
2
by: stewartfip | last post by:
How do I get the ordinal of a form element from a reference to the object itself. For example: <html> <script language="javascript"> function disp_val(objField){ alert(---the ordinal of...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
4
by: Mark Tarver | last post by:
Prompted by a post on Catalan numbers in Qilang, I got into looking at ordinal numbers as defined by John von Neumann - see http://en.wikipedia.org/wiki/Ordinal_numbers 0 = {} 1 = {0} = {{}} 2...
7
by: Dylan Parry | last post by:
Hi folks, I was wondering if there is any way of formatting a date to include the ordinal characters? I've looked at the documentation for DateTime.ToString(), but no where can I find...
9
by: David Jackson | last post by:
Hello, Is there anything in the framework which will format a date to show the ordinal representation of the day value e.g. 28th June 2007 1st August 2007 instead of
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.