数字日期验证 [英] Numeric Date Validation

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

问题描述



似乎古代资料提供了一种数字日期的方法

验证涉及多项测试以确定月份长度;

版本通常由收入者在这里发布。那种代码似乎不必要地花了很长时间。


有一段时间,这里给出了以下方法: -


函数ValidDate(y,m,d){// m = 0..11; ymd整数,y!= 0

with(new Date(y,m,d))

return(getMonth()== m&& getDate() == d)}


它可能仍然是最短的代码。但是,在每个

的情况下,它确实需要创建和处理日期对象。


以下代码的长度增加约50%,但是在我的系统中快四倍

- 它似乎也是正确的。


函数DateOK(Y,M,D){

返回D> 0&& (D< = [,31,28,31,30,31,30,31,31,30,31,30,31] [M] ||

D == 29&& ; M == 2&& Y%4 == 0&&(Y%100> 0 || Y%400 == 0))}


注意检查1< = M< = 12是固有的,并且只有在给定的日期是2月29日时才确定年度的闰度


此外,如果确定

日期是在1901-2099,或者仅仅是2001-2399的两条规则,那么很容易只使用四年规则。


评论?测试??


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://jibbering.com/faq/> Jim Ley的新闻常见问题解答:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,FAQ项目,链接。


It has appeared that ancient sources give a method for Numeric Date
Validation that involves numerous tests to determine month length;
versions are often posted by incomers here. That sort of code seems
unnecessarily long.

For some while, the following approach has been given here :-

function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return (getMonth()==m && getDate()==d) }

and it may remain the shortest code. But it does require, in every
case, the creation and disposal of a Date Object.

The following is about 50% longer in code, but about four times faster
in my system - and it seems to be right, too.

function DateOK(Y, M, D) {
return D>0 && (D<=[,31,28,31,30,31,30,31,31,30,31,30,31][M] ||
D==29 && M==2 && Y%4==0 && (Y%100>0 || Y%400==0) ) }

Note that checking for 1 <= M <= 12 is inherent, and that the Leapness
of the year is only determined if the date given is February 29th.

Also, it is easy to use only the quadrennial rule if it is certain that
dates are in 1901-2099, or only two rules for 2001-2399.

Comments ? Tests ??

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://jibbering.com/faq/> Jim Ley''s FAQ for 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.

推荐答案

John Stockton博士< sp ** @ merlyn.demon .co.uk>写道:
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
以下代码长了约50%,但在我的系统中大约快了四倍 - 而且它似乎也是正确的。

函数DateOK(Y,M,D){
返回D> 0&& (D< = [,31,28,31,30,31,30,31,31,30,31,30,31] [M] ||
D == 29&& M == 2&& Y%4 == 0&&(Y%100> 0 || Y%400 == 0))}


我注意到你生成了一个每个调用的新Array对象。你/ b $ b可以将日期长度数组保存在变量中并重用它。它可能会更快(尽管没有经过测试)。

注意检查1< = M< = 12是固有的,那就是只有在给定的日期是2月29日时才确定年度的闰年。


或者月份无效。怎么样


var __months = [,31,28,31,30,31,30,31,31,30,31,30,31 ]

函数DateOK2(Y,M,D){

var ML = __months [M];

返回D> 0&& ; ML&& (D< = ML ||

D == 29& Y%4 == 0&&(Y%100!= 0 || Y%400 == 0)) }


(M == 2是不必要的,因为ML& D> ML&& D == 29暗示M == 2,但它

可能是合理的,以保持可读性。)


好​​的,在IE中的快速测试显示这个版本是四倍

更快比上面的(仅仅是2月29日测试时,仅仅是两倍于

ValidDate的两倍(不太公平:))。

评论?测试??
The following is about 50% longer in code, but about four times faster
in my system - and it seems to be right, too.

function DateOK(Y, M, D) {
return D>0 && (D<=[,31,28,31,30,31,30,31,31,30,31,30,31][M] ||
D==29 && M==2 && Y%4==0 && (Y%100>0 || Y%400==0) ) }
I notice that you generate a new Array object for each call. You
could keep the date length array in variable and reuse it. It would
probably even be measureably faster (haven''t tested though).
Note that checking for 1 <= M <= 12 is inherent, and that the Leapness
of the year is only determined if the date given is February 29th.
Or if the month is invalid. How about:

var __months = [,31,28,31,30,31,30,31,31,30,31,30,31]
function DateOK2(Y, M, D) {
var ML = __months[M];
return D>0 && ML && (D<=ML ||
D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) }

(M==2 is unnecessary, as ML && D>ML && D==29 implies M==2, but it
might be reasonable to keep it for readability).

Ok, a quick speed test in IE shows this version to be four times
faster than the above (which was only about twice as fast as
ValidDate, when testing only on February 29ths (not really fair :)).
Comments ? Tests ??




关于2月29日:


测试Y%100> 0因Y< = 0而失败,因为-96%100 == -96,而不是4.

更改> 0到!= 0并且有效。


Y =仍然失败0由于某种原因。 ......啊,可能是因为

日期映射Y = 0到1900年,你的测试没有。由于你的b
对手作弊,你默认赢了:)


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http:/ /www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''



About Feb 29ths:

The test Y%100>0 fails for Y<=0, because -96 % 100 == -96, not 4.
Change >0 to !=0 and that works.

It still fails for Y=0 for some reason. ... ah, probably because
Date maps Y=0 to year 1900, and your test doesn''t. Since your
opponent is cheating, you win by default :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


In文章< c2 ************** @ merlyn.demon.co.uk> ;,约翰斯托克顿博士

< sp ** @ merlyn.demon.co .UK>写道:
In article <c2**************@merlyn.demon.co.uk>, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
似乎古代资料提供了一种数字日期验证的方法,其中涉及许多测试以确定月份长度;
版本通常由收入者发布这里。这种代码看起来不必要很长。

有些时候,这里给出了以下方法: -

函数ValidDate(y,m,d) {// m = 0..11; ymd整数,y!= 0
with(new Date(y,m,d))
return(getMonth()== m&& getDate()== d)}

它可能仍然是最短的代码。但在每个
情况下,它确实需要创建和处理日期对象。

以下代码长约50%,但速度提高约四倍
我的系统 - 它似乎也是正确的。

函数DateOK(Y,M,D){
返回D> 0&& (D< = [,31,28,31,30,31,30,31,31,30,31,30,31] [M] ||
D == 29&& M == 2&& Y%4 == 0&&(Y%100> 0 || Y%400 == 0))}

请注意检查1< = M< ; = 12是固有的,并且只有在给定日期是2月29日时才确定年度的闰度。

此外,如果它是四年一次的规则很容易使用确定
日期是在1901-2099,或者只有2001-2399两条规则。

评论?测试??
It has appeared that ancient sources give a method for Numeric Date
Validation that involves numerous tests to determine month length;
versions are often posted by incomers here. That sort of code seems
unnecessarily long.

For some while, the following approach has been given here :-

function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return (getMonth()==m && getDate()==d) }

and it may remain the shortest code. But it does require, in every
case, the creation and disposal of a Date Object.

The following is about 50% longer in code, but about four times faster
in my system - and it seems to be right, too.

function DateOK(Y, M, D) {
return D>0 && (D<=[,31,28,31,30,31,30,31,31,30,31,30,31][M] ||
D==29 && M==2 && Y%4==0 && (Y%100>0 || Y%400==0) ) }

Note that checking for 1 <= M <= 12 is inherent, and that the Leapness
of the year is only determined if the date given is February 29th.

Also, it is easy to use only the quadrennial rule if it is certain that
dates are in 1901-2099, or only two rules for 2001-2399.

Comments ? Tests ??




如果只是闰年检查,以下是否正确?


ayear是4位数年份。


var isLeapYear = ayear%4 == 0&& ayear%100> 0 || ayear%400 == 0;


我总是对评估顺序感到困惑(首先&&然后

||除非使用括号)。


-

Dennis M. Marks
http://www.dcs-chico.com/~denmarks/

用dcsi.net替换domain.invalid

----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000个新闻组 - 19种不同的服务器= -----



Is the following correct for just a leap year check?

ayear is a 4 digit year.

var isLeapYear = ayear%4 == 0 && ayear%100 > 0 || ayear%400 == 0;

I always get confused on the sequence of evaluation (first && and then
|| unless parenthesis are used).

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


Lasse Reichstein Nielsen于2004年4月19日在comp.lang.javascript写了
Lasse Reichstein Nielsen wrote on 19 apr 2004 in comp.lang.javascript:
或者如果月份无效。怎么样

var __months = [,31,28,31,30,31,30,31,31,30,31,30,31] 函数DateOK2(Y,M,D){
var ML = __months [M];
返回D> 0&& ML&& (D< = ML ||
D == 29& Y%4 == 0&&(Y%100!= 0 || Y%400 == 0))}
(M == 2是不必要的,因为ML& D> ML& D = 29意味着M == 2,但是为了便于阅读它可能是合理的好的,在IE中快速测试显示这个版本比上面快四倍(比测试时的ValidDate快两倍)只在2月29日(不公平:))。
Or if the month is invalid. How about:

var __months = [,31,28,31,30,31,30,31,31,30,31,30,31]
function DateOK2(Y, M, D) {
var ML = __months[M];
return D>0 && ML && (D<=ML ||
D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) }

(M==2 is unnecessary, as ML && D>ML && D==29 implies M==2, but it
might be reasonable to keep it for readability).

Ok, a quick speed test in IE shows this version to be four times
faster than the above (which was only about twice as fast as
ValidDate, when testing only on February 29ths (not really fair :)).




为什么要追求速度?

你们都想测试一个巨大的虚假条目的数据库?

通常这个测试仅用于人机界面验证,我认为,

所以速度并不重要。


就个人而言,我会让系统为我做思考:


函数dateOK(Y,M,D){

var da = new日期(Y,M-1,D)

返回Y == da.getFullYear()&&

M-1 == da.getMonth()& ;&

D == da.getDate()

}


这也检查小数,字符串,底片等。

允许正确验证字符串,例如年''02004'',月'' 3.00''

如果年份返回false <100

-

Evertjan。

荷兰。

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



Why this quest for speed?
Do you all want to test an enormous database for false entries?
Usually this test is for human interface validation only, I think,
so speed is not that important.

Personally, I would let the system do the thinking for me:

function dateOK(Y,M,D){
var da = new Date(Y,M-1,D)
return Y == da.getFullYear() &&
M-1 == da.getMonth() &&
D == da.getDate()
}

This also checkes for decimals, strings, negatives, etc.
correctly validating strings are allowed, like year ''02004'',month ''3.00''
returns false if year<100

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


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

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