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

Compare 2 dates form

P: n/a
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??
Thanks.......
Oct 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Lee
Rimuen said:

Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??


It depends on what format they use to type the date.
If they use YYYY-MM-DD, all you need to do is compare the strings.
If they use some clumsier format, it still may be simplest to just
reformat it and compare.

Oct 20 '05 #2

P: n/a
First of all you must validate input text.

if your date formst is mm-dd-yyyy then your regex will be like this:

function ToDate( value ) {

var re=/^[0-9]{1,2}(-)[0-9]{1,2}(-)([0-9]{4})$/;

if (!re.test(value))
return null;

var dateItems = value.split (/(\/|-|\.)/);
var month = parseInt(dateItems[0],10)-1;
var day = parseInt (dateItems[1],10);
var year = parseInt(dateItems[2],10);

var dtTest = new Date (year, month, day);

return dtTest;
}

After that compare two dates:
var firstDate = ToDate(myform["firstDate"].value)
var secondDate = ToDate(myform["secondDate"].value)

if (!firstDate || !secondDate)
alert("some date wrong!");

if (firstDate < secondDate)
//do something here
else
alert("First date value must be less than second date.");

Best, Ed.

Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??
Thanks.......


Oct 20 '05 #3

P: n/a
VK

Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.


with (document.yourForm) {
if (Date.parse(DateTwo.value) > Date.parse(DateOne.value)) {
//OK
}
else {
//Cancel submission
}
}

The date formats commonly understood by the date parser can be checked
here (despite this is Microsoft, nothing terribly wrong or provocative
on that page) :
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthparse.asp>

Oct 20 '05 #4

P: n/a
Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??


I have a date library which can easily compare dates in any given format:
http://www.javascripttoolbox.com/date/

Depending on your specific situation, you could also write code specific to
your page which would be smaller and more concise, which I'm sure others
will suggest.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 20 '05 #5

P: n/a
Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??
Thanks.......


function date2isLaterThanDate1(date1,date2){
return date2>date1;
}

But in which format is the date entered into the form?

Mick
Oct 20 '05 #6

P: n/a
Thanks all for answering, but my skill in Java is bad....
How could this script be placed in this html ?
date format is dd.mm.yyyy.
<form method="post" action="default.asp">
<input type="text" size="15" name="Date1">
<input type="text" size="15" name="Date2">
<input type="submit" value="Send">
</form>

"Mick White" <mw***********@rochester.rr.com> wrote in message
news:ET*******************@twister.nyroc.rr.com...
Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??
Thanks.......


function date2isLaterThanDate1(date1,date2){
return date2>date1;
}

But in which format is the date entered into the form?

Mick

Oct 20 '05 #7

P: n/a
"Rimuen" <po**@pricom.no> wrote in message
news:dj**********@services.kq.no...
Thanks all for answering, but my skill in Java is bad....
How could this script be placed in this html ?
date format is dd.mm.yyyy.
<form method="post" action="default.asp">
<input type="text" size="15" name="Date1">
<input type="text" size="15" name="Date2">
<input type="submit" value="Send">
</form>


[snip]

Perhaps like this.

<html>
<head>
<title>DateComp.htm</title>
<script type="text/javascript">
function date2isLaterThanDate1(date1,date2) {
// "dd.mm.yyyy"; for example, "01.01.2005" or "31.12.2005".
if (date1.length != 10 || (date2.length != 10)) return false;
date1 = date1.substr(6) + date1.substr(3,2) + date2.substr(0,2);
date2 = date2.substr(6) + date2.substr(3,2) + date2.substr(0,2);
alert(date2 + " > " + date1 + " = " + (date2 > date1));
return (date2 > date1);
}
</script>
</head>
<body>
<form method="post" action="default.asp"
onsubmit="return date2isLaterThanDate1(this.Date1.value,this.Date2. value)">
<input type="text" size="15" name="Date1">
<input type="text" size="15" name="Date2">
<input type="submit" value="Send">
</form>
</body>
</html>

Oct 20 '05 #8

P: n/a
JRS: In article <dj**********@services.kq.no>, dated Thu, 20 Oct 2005
15:42:29, seen in news:comp.lang.javascript, Rimuen <po**@pricom.no>
posted :
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.
Anyone have a script for this ??
Thanks.......


Read the newsgroup FAQ; see below.

If Norway uses ISO 8601 dates, you can (within 1000-9999 AD) compare
them as strings directly. If you accept ##?##?#### dates, then you must
stipulate just one of DD?MM?YYYY or MM?DD?YYYY, or else there can be
error. If the months are in Norwegian, you'll have to deal with that
yourself, or ask in news:no.it.programmering.javascript.

--
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.
Oct 20 '05 #9

P: n/a
JRS: In article <11*********************@g47g2000cwa.googlegroups. com>,
dated Thu, 20 Oct 2005 07:49:40, seen in news:comp.lang.javascript,
Gomolyako Eduard <ed**************@gmail.com> posted :
First of all you must validate input text.

if your date formst is mm-dd-yyyy then your regex will be like this:

function ToDate( value ) {

var re=/^[0-9]{1,2}(-)[0-9]{1,2}(-)([0-9]{4})$/;

if (!re.test(value))
return null;

var dateItems = value.split (/(\/|-|\.)/);
var month = parseInt(dateItems[0],10)-1;
var day = parseInt (dateItems[1],10);
var year = parseInt(dateItems[2],10);

var dtTest = new Date (year, month, day);

return dtTest;
}
You appear to be assuming FFF, which is unreasonable when responding to
someone apparently in NO.

That code does not validate the date properly. Unary + can be used
instead of parseInt.

There's no need to convert to Date Objects, which is slow; instead,
return (year*100+month)*100+day.

Don't top-post; don't over-quote.

Read the newsgroup FAQ.
After that compare two dates:
var firstDate = ToDate(myform["firstDate"].value)
var secondDate = ToDate(myform["secondDate"].value)

if (!firstDate || !secondDate)
alert("some date wrong!");
ISTM that that would give a false error message if either date
corresponds to 1970-01-01 GMT, if it worked at all. Using !+firstdate
seems better.

Rimuen wrote:
Have two text form with dates i need to compare before submitting, the
second should always be a date later the first one.


--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news.groups>.
Oct 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.