473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing dates

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

Similar topics

4
by: F | last post by:
Hi I have posted the question few days back about problem in inserting the dates in SQL server and thankful to them who replied. That was solved and this is a nice solution....
2
by: Mike N. | last post by:
I am currently using a function to validate a form on the client side (see code below). At the end of the function, I would like it to also compare a startDate against an endDate to ensure that the...
2
by: Duppypog | last post by:
I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database is 11/5/2003 with today being 11/6/2003. Can...
2
by: Philip Townsend | last post by:
I am having difficulty with a simple routine as follows: public static bool VerifyExpirationDate(DateTime date) { if(date>=DateTime.Now)return true; else return false; } The problem is that...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
2
by: ameshkin | last post by:
Hi GUys, Im trying to compare two dates in MYSQL. But its not treating the dates as numbers, but as strings. I try using strtotime but that did not work. Basically, if the last comment is...
12
by: colincolehour | last post by:
I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the website only shows 3 letter month name and the...
4
by: cheryl | last post by:
I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the user will check first the room availability. The...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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,...

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.