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

Date Comparison

P: n/a


Hi there !!!

I would like to know how to compare dates in javascript.
var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);
var todaysdate = new Date();

if(sdate < todaysdate)
{
alert("Submissionn Date should not be less than current date");
return false;
}

if(sdate < odate)
{
alert("Submissionn Date should not be less than opening date");
return false;
}
My above comparison does'nt work. can u suggest some way???

Mar 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tiya wrote:
I would like to know how to compare dates in javascript.
To compare Date objects, compare their .getTime() values.
var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);


This assumes the input fields contain date strings that can be automatically
parsed by the browser. Bad assumption.
Instead, either require the user to enter dates in a specific format or try
to parse a variety of common formats to retrieve the Date object.
Using the lib here: http://www.javascripttoolbox.com/lib/date/
you can easily parse a date string using Date.parseString()

So your code could look something like this:

var sdate = Date.parseString(theform.SubmissionDate.value);
if (sDate.isBefore(new Date()) {
...
}

Hope that helps.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 13 '06 #2

P: n/a
Matt Kruse wrote:
Tiya wrote:
I would like to know how to compare dates in javascript.


To compare Date objects, compare their .getTime() values.


That is not necessary for relational operators. Implicit type conversion
to Number would call their .valueOf() method which returns the same
value .getTime() returns. See ECMAScript Edition 3, section 11.8.
PointedEars
Mar 13 '06 #3

P: n/a
JRS: In article <11*********************@i40g2000cwc.googlegroups. com>,
dated Mon, 13 Mar 2006 02:56:31 remote, seen in
news:comp.lang.javascript, Tiya <Sw*********@gmail.com> posted :
I would like to know how to compare dates in javascript.

var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);
var todaysdate = new Date();

if(sdate < todaysdate)
{
alert("Submissionn Date should not be less than current date");
return false;
}

if(sdate < odate)
{
alert("Submissionn Date should not be less than opening date");
return false;
}
My above comparison does'nt work. can u suggest some way???

Slovenly ---^

The comparison code works correctly, and compares dates as desired.
Use, say,
alert(sdate + "\n" + odate + "\n" + todaysdate)
to see what your date strings have been interpreted as. Remember that
strings ##/##/## are liable to be taken as Merkin FFF, and perhaps in
the wrong centade.

Since real users may input incorrectly, put the dates in the alerts.

If sdate is a pure Date, and represents today, the message for the
comparison with todaysdate is questionable.

Read the newsgroup FAQ; see below.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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.
Mar 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.