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

input day in the past

P: n/a
Hello,

If have a HTML form where a user can type a date, this date must be today,
or in the future. To test it i made this javascript

function validate()
{
var cdate_h = form.elements["cdate"].value;
if (cdate_h.length == 0)
{
alert("completion date");
return;
}

calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();

var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date
format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);

var userdate = strYear * 10000 + strMonth * 100 + strDay;

if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}

When I debug the variables userdate and today it looks fine, but the if
statement does not work WHY ??????

Can someone help me ? Many thanks in advance.

Richard

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"avcc" <av**@freeler.nl> writes:
var userdate = strYear * 10000 + strMonth * 100 + strDay;
In this, strDay is a string, so it as appended to userdate to form a
new string.
If strYear is "2003", strMonth is "10" and strDay is "18", then the result
is
(20030000 + 1000 + "18") = "2003100018"
It is not the expected 20031018.

If you change the above line to
var userdate = strYear * 10000 + strMonth * 100 + (+strDay);
of
var userdate = strYear * 10000 + strMonth * 100 + strDay * 1;
then userdate becomes a number, and the one you expect.
if (parseFloat(userdate) < parseFloat(today))


You can drop parseFloat on today, it is already a number. If you make
the fix above, so is userdate.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"avcc" <av**@freeler.nl> wrote in message
news:Nn**********************@amsnews03.chello.com ...
If have a HTML form where a user can type a date, this date
must be today, or in the future. To test it i made this javascript
Generally:-
<URL: http://www.merlyn.demon.co.uk/js-dates.htm >
function validate()
{
var cdate_h = form.elements["cdate"].value;
if (cdate_h.length == 0)
{
alert("completion date");
return;
}
calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();
var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);
Specifically, the values of strMonth, strYear and strDay are strings. In
the next line:-
var userdate = strYear * 10000 + strMonth * 100 + strDay;
- the use of the multiplication operator will automatically type-convert
strYear and strMonth to numbers but the + operator is a duel purpose
addition and string concatenation operator and if either of its operands
is a string it does concatenation. strDay is a string so even if
((strYear*10000)+(strMonth*100)) produced a numeric value the final +
operator will have that value converted into a string and concatenate
strDay to it. Later using parsFloat (on an integer?) will have that
string converted back to a number for the comparison but that string is
one to two characters longer than you are expecting thus the number is
at least 10 to 100 times larger.
if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}


Richard.
Jul 20 '05 #3

P: n/a
JRS: In article <Nn**********************@amsnews03.chello.com>, seen
in news:comp.lang.javascript, avcc <av**@freeler.nl> posted at Sat, 18
Oct 2003 10:14:05 :-
If have a HTML form where a user can type a date, this date must be today,
or in the future. To test it i made this javascript

function validate()
{
var cdate_h = form.elements["cdate"].value;
if (cdate_h.length == 0)
{
alert("completion date");
return;
}

calDate = new Date();

var day = calDate.getDate();
var month = calDate.getMonth() + 1;
var year = calDate.getFullYear();

var today = year * 10000 + month * 100 + day;

var pos1=cdate_h.indexOf("-"); // date
format YYY-MM-DD
var pos2=cdate_h.indexOf("-",pos1+1);
var strYear=cdate_h.substring(0,pos1);
var strMonth=cdate_h.substring(pos1+1,pos2);
var strDay=cdate_h.substring(pos2+1);

var userdate = strYear * 10000 + strMonth * 100 + strDay;

if (parseFloat(userdate) < parseFloat(today))
{
alert ("Completion date can not be in the past");
return;
}
}

When I debug the variables userdate and today it looks fine, but the if
statement does not work WHY ??????

Can someone help me ? Many thanks in advance.

The last thing that is wrong is that you did not tell us explicitly how
it did not work. Perhaps it works for some combinations of dates.

A further error is that, in English, you should be writing "cannot"
instead of "can not". The words "can not" really mean "possibly not",
whereas "cannot" means "certainly not". This is a European secret which
the Americans have not yet discovered.

The previous thing is that, after finding (I suppose) that the alert did
or did not occur, you should immediately have preceded it with, for
example, alert(userdate+'\n'+today) , in order to see what had
happened.

The explanation of what happens has already been given.

The code is too long. AIUI, cdate_h is a string in YMD format.
Assuming it has a 4-digit year, consider

S = "2003-01-05"
if ( new Date() - new Date(S.replace(/-/g, '/')) > 0 )
alert("Completion date cannot be in the past")

If it may be a 2-digit year,
S = S.replace(/(\d+)\D+(\d+)\D+(\d+)/, '$2/$3/$1')
will generate a DDF M/D/Y which I believe all Javascript will
understand.

But if it will be a 2-digit year, then, for a while, use
S = '20'+S.replace(/-/g, '/')

--
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> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.