473,466 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5845
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
2
by: Gernot Frisch | last post by:
bool isOlderThanToday(const char* pDate) { // How to do this? } void main() { const char* pDate = "29-Feb-2005"; if (isOlderThanToday(pDate))
1
by: - | last post by:
what is the function to check whether a date, eg 2005-02-44, exists? in php, there is a checkdate() function.
17
by: Franc Zabkar | last post by:
My D-Link DSL-302G modem/router has a real-time clock whose settings are volatile. To avoid hand keying the date/time via the modem's JS interface, I wonder if there is a way to copy the JS code to...
1
by: Thomas | last post by:
Hi all, i have to check if a value is a valid date format depending on a "format string" the user specified. For example: Format String: "dd/mm/yyyy" Date Value: "01/12/2006" Valid: Yes! ...
0
by: henk | last post by:
Hi, I want to validate/"autocomple" a string from a textbox. The string could be eg: "1 2 3" "01-02-2003" "1-feb-2003" etc. I found one solution for this:
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
4
by: norma.j.hildebrand | last post by:
I have a database that has a field (performance standard), every year the standard changed which was not a problem since we start out each year with a blank database and just change the standards...
9
by: eggie5 | last post by:
How would I check if a string is a number? e.g: 'asdf2' =false '34' =true 'asf' =false '0' =true
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.