By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Comparing dates

P: n/a
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yeah what you got there is a comparison of the date object's string value
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.

Jul 20 '05 #2

P: n/a
In article <40***********************@news.frii.net>,
ha**********@spray.no enlightened us with...
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/...ate_parse.html
http://members.ozemail.com.au/~dcrom...pt/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

Jul 20 '05 #3

P: n/a
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.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.