469,332 Members | 6,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Check for valid date (eg: 31 Feb)

Hi,

Assuming the date format is in 31-Feb-2006

<head>
<script language="javascript">
function t()
{
mill=new Date(2006, 01, 31) ;

alert(mill.getMonth()+1);
alert(mill.getDate());
}
</script>
</head>
<body onLoad="t()"></body>

By right 31-Feb is an invalid date,

How do I go about showing an error?

I thought of using:

mill = new Date(2006, 01, 31) ;

if(mill==false)
{
alert("Invalid Date");
}

But this doesnt work.

Any suggestions please?

Thanks

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Dec 4 '06 #1
9 5404
User wrote on 05 dec 2006 in comp.lang.javascript:
function t()
{
mill=new Date(2006, 01, 31) ;

alert(mill.getMonth()+1);
alert(mill.getDate());
}
<script type='text/javascript'>

alert( test(2006,2,31) );

function test(y,m,d){
return new Date(y,m-1,d).getMonth() == m-1;
};

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 4 '06 #2
Nice.

Thanks.
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn*******************@194.109.133.242...
User wrote on 05 dec 2006 in comp.lang.javascript:
>function t()
{
mill=new Date(2006, 01, 31) ;

alert(mill.getMonth()+1);
alert(mill.getDate());
}

<script type='text/javascript'>

alert( test(2006,2,31) );

function test(y,m,d){
return new Date(y,m-1,d).getMonth() == m-1;
};

</script>

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


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Dec 4 '06 #3

Evertjan. wrote:
User wrote on 05 dec 2006 in comp.lang.javascript:
function t()
{
mill=new Date(2006, 01, 31) ;

alert(mill.getMonth()+1);
alert(mill.getDate());
}

<script type='text/javascript'>

alert( test(2006,2,31) );

function test(y,m,d){
return new Date(y,m-1,d).getMonth() == m-1;
};

</script>
I think it is generally accepted that two of the three date components
should be checked - say date and month, though month and year would
probably be better there may be a need to avoid getFullYear.

--
Rob

--

Dec 5 '06 #4
In comp.lang.javascript message
<11**********************@80g2000cwy.googlegroups. com>, Mon, 4 Dec 2006
16:58:04, RobG <rg***@iinet.net.auwrote:
>
Evertjan. wrote:
>User wrote on 05 dec 2006 in comp.lang.javascript:
function t()
{
mill=new Date(2006, 01, 31) ;

alert(mill.getMonth()+1);
alert(mill.getDate());
}

<script type='text/javascript'>

alert( test(2006,2,31) );

function test(y,m,d){
return new Date(y,m-1,d).getMonth() == m-1;
};

</script>

I think it is generally accepted that two of the three date components
should be checked
Yes, when D is unbounded.
- say date and month, though month and year would
probably be better there may be a need to avoid getFullYear.
The test would be quicker if done in UTC, since both new Date(Y, M, D)
and getMonth() require contemplation of the season.

Even getUTCDate() requires a complex calculation, though that should be
programmed in a fast language.

So how about
function test(y,m,d){
return d>0 && d<32 && new Date(y,m-1,d).getMonth() == m-1;
};

where the d tests may be faster than .getDate()==d ? Then it might be
better to test m similarly, so that the complex part is only needed for
Dates whose validity depends on Month.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.
Don't quote more than is needed, and respond after each quoted part.
Dec 5 '06 #5
Dr J R Stockton wrote on 06 dec 2006 in comp.lang.javascript:
So how about
function test(y,m,d){
return d>0 && d<32 && new Date(y,m-1,d).getMonth() == m-1;
};
function test(y,m,d){
return Math.abs(d)<365 && new Date(y,m-1,d).getMonth()==m-1;
};

;-)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 6 '06 #6
Evertjan. wrote:
Dr J R Stockton wrote on 06 dec 2006 in comp.lang.javascript:
> function test(y,m,d){
return d>0 && d<32 && new Date(y,m-1,d).getMonth() == m-1;
};
function test(y,m,d){
return Math.abs(d)<365 && new Date(y,m-1,d).getMonth()==m-1;
};
;-)
var months = [31,28,31,30,31,30,31,31,30,31,30,31];
function test(y,m,d) {
return y>0 && m>0 && m<=12 && (d<=months[m-1] || (m==2 && d==29 && ((y%4==0
&& y%100!=0) || (y%400==0)) ) );
};

Around 6-7x times faster in IE, and 70x faster in FF.
I'll take speed over brevity any day.
;-)

(There may be fringe cases not handled correctly by my example (such as
y=-1), but it could probably be expanded slightly and still retain the huge
speed increase by not creating a new Date object).

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 6 '06 #7
Matt Kruse wrote on 06 dec 2006 in comp.lang.javascript:
Evertjan. wrote:
>Dr J R Stockton wrote on 06 dec 2006 in comp.lang.javascript:
>> function test(y,m,d){
return d>0 && d<32 && new Date(y,m-1,d).getMonth() == m-1;
};
function test(y,m,d){
return Math.abs(d)<365 && new Date(y,m-1,d).getMonth()==m-1;
};
;-)
My point is that only d>364 or d<-364 can give an error in
new Date(y,m-1,d).getMonth()==m-1.
var months = [31,28,31,30,31,30,31,31,30,31,30,31];
function test(y,m,d) {
return y>0 && m>0 && m<=12 && (d<=months[m-1] || (m==2 && d==29 &&
((y%4==0
&& y%100!=0) || (y%400==0)) ) );
};
(y%400==0)

y%400==0
Around 6-7x times faster in IE, and 70x faster in FF.
I'll take speed over brevity any day.
;-)
That depends on the use.

This function will be used usually in user input validation,
where timing is not important, and repetitive calling is absent.
(There may be fringe cases not handled correctly by my example (such
as y=-1),
you have y>0 !
but it could probably be expanded slightly and still retain
the huge speed increase by not creating a new Date object).

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 6 '06 #8
In comp.lang.javascript message
<Xn********************@194.109.133.242>, Wed, 6 Dec 2006 05:02:55,
Evertjan. <ex**************@interxnl.netwrote:
>Dr J R Stockton wrote on 06 dec 2006 in comp.lang.javascript:
>So how about
function test(y,m,d){
return d>0 && d<32 && new Date(y,m-1,d).getMonth() == m-1;
};

function test(y,m,d){
return Math.abs(d)<365 && new Date(y,m-1,d).getMonth()==m-1;
};

;-)
But test(2000, 1, -335) -true.

If there is a test on d, it should be d<32 since that uses fewer
characters, is faster for days 32 and up, ** and needs less thought **.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Dec 7 '06 #9
In comp.lang.javascript message <el********@news3.newsguy.com>, Tue, 5
Dec 2006 23:52:43, Matt Kruse <ne********@mattkruse.comwrote:
>var months = [31,28,31,30,31,30,31,31,30,31,30,31];
function test(y,m,d) {
return y>0 && m>0 && m<=12 && (d<=months[m-1] || (m==2 && d==29 && ((y%4==0
&& y%100!=0) || (y%400==0)) ) );
};

Around 6-7x times faster in IE, and 70x faster in FF.
I'll take speed over brevity any day.
;-)
Then why test for m==2 ? Only for February can d fail the array test
and be 29. That test costs both speed and brevity.

With a comma after the first bracket, a subtraction is saved.

The "Julian" test *may* be faster as !(Y&3).
>(There may be fringe cases not handled correctly by my example (such as
y=-1),
We generally presume that dates should be yyyy-mm-dd or variants,
implying Y>=0. But you omitted to test d>0.
but it could probably be expanded slightly and still retain the huge
speed increase by not creating a new Date object).
OTOH, often a Date Object will be wanted.
Remember, if a Date Object is used for pure-date work, replace
new Date(Y, M, D) with new Date(Date.UTC(Y, M, D))
and use UTC methods for a considerable speed gain at least in IE4.
Thought : Given D, both getMonth and getDate require determination of
whether the date is in Summer Time. Is any browser intelligent enough
to cache the state of Summer Time (which could be detected by speed
tests) ? getMilliseconds and getSeconds do NOT need the Summer State,
and getMinutes only rarely does - do browsers use that?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/>
Dec 7 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dan S | last post: by
2 posts views Thread by Gernot Frisch | last post: by
1 post views Thread by - | last post: by
17 posts views Thread by Franc Zabkar | last post: by
1 post views Thread by Thomas | last post: by
reply views Thread by henk | last post: by
4 posts views Thread by norma.j.hildebrand | last post: by
9 posts views Thread by eggie5 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.