JRS: In article <Wa********************@comcast.com>, seen in
news:comp.lang.javascript, Mike <mi*****************@synovic.com> posted
at Sun, 18 Jan 2004 20:23:32 :-
Yeah what you got there is a comparison of the date object's string value
AIUI, he has no Date object. ECMA-262, 15.9.2.
agains the value of the text field. That will never work. You need to parse
the text the user entered and create a new Date object based on those
day/month/year values (see the javascript documentation for how to
manipulate and create Date objects). This gets to be a pain if you allow
international date formats because you have to account for all the different
ways to enter a date (dd/mm/yy, dd/mm/yyyy, mm/dd/yy, etc)
You can prob find some free date scripts on the net. Try scriptsearch.com.
If you read the FAQ *before* posting an "answer", you will be better
informed.
"International date formats" is evidently an American term, intended to
mean "foreign" ones. Do not use it. There is exactly one full
International numeric Gregorian date format, YYYY-MM-DD, given by ISO
8601 (a Federal standard, I believe). The word you should have used is
"various".
It is not *necessary* to parse anything. The input string *can* be
required in, or converted to, standard form, and then compared as a
string with an ISO representation of new Date().
One cannot allow for all the date forms you list, without further
information; for the first 12 days of a month, dd/mm/yy and mm/dd/yy
cannot be told apart by mere inspection. However, yyyy?mm?dd is safe,
because (a) it is a standard, (b) *no-one* uses yyyy?dd?mm.
I cannot see in ECMA-262 a specification of the format of a numeric date
string; mm/dd/yy (FFF) is common, but it may be unwise to assume that it
will hold everywhere in perpetuity. Therefore, I generally prefer to
split the date string (in known order) into its numeric fields and apply
them in new Date(,,), unless the string is known to be YYYY?MM?DD.
S = "23/04/1616"
T = S.split(/\D+/)
T = new Date(T[2], T[1]-1, T[0])
D = new Date()
document.write( "Bill is not yet dead : ", D < T )
Since the OP is generating the date string to be checked against today,
he can be sure of its format. He could instead generate the number of
milliseconds since Epoch.
The OP's function seems not to be able to return true.
For a validation function, ISTM clearer to use the structure
function CheckOK()
var OK = <test as needed>
if (!OK) { < actions as needed > }
return OK }
which ensures a defined return value.
Be aware that comparison with today's date needs consideration; it *may*
be better to use tomorrow's : T = new Date(T[2], T[1]-1, T[0] + 1)
Date() gives a string in an ill-specified form; instead, new Date()
should be used.
Remember that a page served by a U.S. server in the local afternoon will
be received in New Zealand on the following morning by their calendar;
and a brand new one from NZ will generally arrive in HI on the previous
date.
--
© 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.