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. 12 11730
"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
>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
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)
>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
"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
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.
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
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
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?)
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.
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:
...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |