几个日期查询 [英] A couple of date queries

查看:99
本文介绍了几个日期查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我希望能够以dd格式显示日期。


如果在我的脚本文件中我有:


var dd = DayOfMonthArray [now.getDate()];


在我的html页面中我有:


< script type =" ; text / javascript">

document.write(dd);

< / script>


然后是日期显示在d fomat中,所以不是01我得到1.

但是如果在我的脚本文件中我有:


var DayOfMonthArray = new Array

(01,02,03,04,05,06,07,08,

09,10,11,12,13,14,15,16,17, ; 18,19,20,21,22,

" 23",24,25,26 ;," 27"," 28"," 29"," 30英寸;," ; 31");

var dd = DayOfMonthArray [now.getDate()];

并且在我的html页面中我有:


< script type =" text / javascript">

document.write(dd);

< / script>


然后它将日期加1!所以不是01而是02!为什么?什么'

最好的解决方法呢?

2)我希望能够以yy格式显示年份。 now.getYear()

以yyyy格式显示yeat;什么是获得它的最佳方式

以yy格式显示?


Dave

1) I want to be able to display the date in dd format.

If in my script file I have:

var dd = DayOfMonthArray[now.getDate()];

and in my html page I have:

<script type="text/javascript">
document.write(dd);
</script>

Then the date displays in d fomat, so instead of 01 I get 1.
But if in my script file I have:

var DayOfMonthArray = new Array
("01","02","03","04","05","06","07","08",
"09","10","11","12","13","14","15","16","17","18", "19","20","21","22",
"23","24","25","26","27","28","29","30","31");
var dd = DayOfMonthArray[now.getDate()];
and in my html page I have:

<script type="text/javascript">
document.write(dd);
</script>

Then it adds 1 to the date!! So instead of 01 I get 02! Why? And what''s
the best way of getting round it?
2) I want to be able to display the year in yy format. now.getYear()
displays the yeat in yyyy format; what is the best way to get it to
display in yy format?

Dave

推荐答案

哎呀!上面的第3行应该是:


var dd = now.getDate();

Whoops! Line 3 above should have read:

var dd = now.getDate();


Dave Rado写道在13 mei 2006 in comp.lang.javascript
Dave Rado wrote on 13 mei 2006 in comp.lang.javascript:
var DayOfMonthArray = new Array
(" 01"," 02", 03,04,05,06,07,08,/,09,10,11, ; 12,13,14,15,16,17,18,19,20,21, ; 22,23,24,25,26,27,28,29,30, 31";
var dd = DayOfMonthArray [now.getDate()];
var DayOfMonthArray = new Array
("01","02","03","04","05","06","07","08",
"09","10","11","12","13","14","15","16","17","18", "19","20","21","
22", "23","24","25","26","27","28","29","30","31");
var dd = DayOfMonthArray[now.getDate()];




数组的第一个元素的编号为0,

所以1日结果是02



在31日结果未定义。


= =======================



尝试以上内容:


var dd = new Date();

dd = dd.getDate();

dd =(dd< 10)?''0''+ dd:''''+ dd;


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)



The first element of an array has number 0,
so on the 1st the result is 02
and
on the 31st the result wil be undefined.

========================

But wy use an array?

Try for the above:

var dd = new Date();
dd = dd.getDate();
dd = (dd<10)?''0''+dd:''''+dd;

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


谢谢,Lee 。


1)当我在我的html页面中使用你的代码时,它有效。但是当我把你的

代码放到我的脚本文件中时(在我的

html文件中只有document.wite语句),我得到一个对象没有发现"错误。知道我能做什么吗?b $ b做错了什么?在我的脚本文件中,我有:


函数zeroPad(n){

return(n> 10)?n:" 0" + n; < br $>
}


var now = new Date()

var yy = zeropad(now.getFullYear()%100);


相反,我有我的scipt文件:


var yy = now.getFullYear()%100;

var yy =(yy< 10)?''0''+ yy:''''+ yy


...那么它有效!即它似乎不喜欢这个函数,当它在脚本文件中是'b
时,虽然它对它的功能感到满意

改为html页面!很奇怪。


2)%100位意味着什么?


3)鉴于你所说的getYear()返回一个不可靠的数字

的数字,我怎样才能返回一个保证的四位数年份?

Dave

Thanks, Lee.

1) When I use your code in my html page, it works. But when I put your
code into my script file (with just the "document.wite" statement in my
html file), I get an "Object not found" error. Any idea what I could
have done wrong? In my script file I have:

function zeroPad(n) {
return (n>10)?n:"0"+n;
}

var now = new Date()
var yy=zeropad(now.getFullYear()%100);

If instead I have in my scipt file:

var yy=now.getFullYear()%100;
var yy = (yy<10)?''0''+yy:''''+yy

... then it works! I.e. it doesn''t seem to like the function when it''s
in the script file, although it''s happy with the function when it''s on
the html page instead! Weird.

2) What does the %100 bit mean?

3) Given what you said about getYear() returning an unreliable number
of digits, how can I return a guaranteed four-digit year?
Dave


这篇关于几个日期查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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