Comparing dates

H

Harlan Dorado

Hi,
I'm trying to write a function to compare a date posted from a text
field in
an asp-page to the date today. The idea is that the date entered in the
form
field cannot be in the past. This is my function:

function chkDate(){
var dDate = Date()
if(document.frmDate.txtDate.value<dDate){
alert("You've entered a date in the past, please try again.");
return false;
}
}

It seems to me that my script doesn't understand that I enter a date,
because I always receive the alert - no matter what I put in the text
field.
I probably have to convert the value or something. Or perhaps I'm
testing
the wrong property....
Being a novice in javascript I can certainly sit here and wonder, but
some
of you guys probably spot immediately what I do wrong and what needs to
be
changed.

Thanks for any reply.

Harlan



______________
the woim toins
 
K

kaeli

function chkDate(){
var dDate = Date()
if(document.frmDate.txtDate.value<dDate){

You're comparing a string to a date.

Here's a hint.
http://www.devguru.com/Technologies/ecmascript/quickref/date_parse.html
http://members.ozemail.com.au/~dcrombie/javascript/chap09.html

I don't use these functions, so I don't know how cross-browser they are.

Don't forget to check for no value (blank text field) before trying to
compare.

--
--
~kaeli~
Jesus saves, Allah protects, and Cthulhu thinks you'd make
a nice sandwich.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top