Ordinal(st,nd,rd,th)在javascript中出现 [英] Ordinal (st, nd, rd, th) dates in javascript

查看:90
本文介绍了Ordinal(st,nd,rd,th)在javascript中出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我的第一篇帖子...我想在网页上添加一个自动更新日期字段

。我发现下面的例子在互联网上工作

出色的除了我想要一个序数日期(例如第1,第2,第3,第4)

而不是红衣主教日期(例如1,2,3,4)。


我怎么能用这一小段javascript代码呢?


< ; script language =" JavaScript">

var now = new Date();

var monNames = new

Array(" 1月,2月,3月,4月,5月,6月,7月,8月,9月,10月, Novem ber,12月);

document.write(" Last updated:" + now.getDate()+"" +

monNames [now.getMonth()] +"" + now.getFullYear());

< / script>


提前致谢建议和东西。

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.

推荐答案

" Joseph Numpty"写了
"Joseph Numpty" wrote
我的第一篇文章...我想在网页上添加一个自动更新日期字段
。我发现下面的例子在互联网上工作得非常好,除了我想要一个序数日期(例如第1,第2,第3,第4)
而不是基数日期(例如1,2,3 ,4)。

我怎么能用这么少的javascript代码呢?

< script language =" JavaScript">


删除语言。请改用。像这样:

< script type =" text / javascript">

var now = new Date();


这是当前日期,而不是最后修改日期。

var monNames = new

数组(" 1月" ,2月,3月,4月,5月,6月,7月,8月,9月份/ b $ b bmber, " 10月","十一月","十二月"); document.write(" Last updated:" + now.getDate()+"" +


制作:

document.write( 上次更新:+ ordi(now.getDate())+等等。


并且具有如下函数:

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> ;
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>




例如。

--Iv



For example.
--Iv


> On Thu ,2004年9月9日11:23:31 +0200,Ivo< no@thank.you>写道:
>On Thu, 9 Sep 2004 11:23:31 +0200, "Ivo" <no@thank.you> wrote:

< snip>
例如。
- Iv
<snip>
For example.
--Iv




非常感谢你的帮助。你已经很好地解决了我的问题。





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

Joe


在9/9/04 10:23 am,Ivo写道:
On 9/9/04 10:23 am, Ivo wrote:
并且具有如下函数:
函数ordi(n){
var s =''th'';
if(n === 1)s =''st'';
if(n === 2)s =''nd'';
if(n === 3)s =''rd'';
返回n + s;
}
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;
}




这样会更好:


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'';

返回n + s;

}
< br $> b $ b -

Philip Ronan
ph * **********@virgin.net

(如果通过电子邮件回复,请删除z)



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)


这篇关于Ordinal(st,nd,rd,th)在javascript中出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆