Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date compare

Status
Not open for further replies.

pikasat

Technical User
Jun 7, 2002
12
HR
I'm working on a page with two dates displayed in
two textfialds (startDate and endDate).
User enters them from js calendar. After that, and before
db search, dates have to be validated -

startDate > today
endDate > startDate
startDate <> endDate

I written function as below:

Code:
function search()
{
valid = true;
var startdate = new Date(form.datum1.value)
var enddate = new Date(form.datum2.value)
var today = new Date ()
					

	if (today > startdate)
	{
		alert ('Please, enter correct "Date from" !');
		datum1.focus();
		return false;
	}
   
	if (startdate == enddate)
	{
		alert ('Please, enter correct "Date to" !');
		datum2.focus();
		return false;
	}
	
	if (startdate > enddate)
	{
		alert ('Please, enter correct "Date to" !');
		datum2.focus();
		return false;
	}
	
  else
    frmL.action = "rezultati-rez.asp"
    method = "POST"
    frmL.submit()
	
}

It work OK only for third case, and not for other two. Does anybody have solution ?
 
I wrote a function some time back that gets the ordinal number (some people incorrectly refer to it as Julian date).

The ordinal number is literally the number of days within the year, so...
Jan. 1 = 1
Jan. 2 = 2
..
Feb. 1 = 32
.
.
.
Dec. 31 = 365 (or 366 for leap years)

At that point, you could literally check the ordinal values to verify values entered against "today."

It takes in 3 values (month, date, 4-digit year).
It then returns the ordinal value.
It may or may not help you, but here you go anyway. :)

function ordinal(m,d,y)
{
var value=0;
var i;
var leapYear=0;

y=parseInt(y);
d=parseInt(d);
m=parseInt(m);
if (y % 4 == 0) leapYear=1;
for (i=1; i<m; i++)
{
if (i==9 || i==4 || i==6 || i==11)
{ value+=30; }
else if (i==2) value+=(28+leapYear);
else value+=31;
}// end for i
value+=d;
return parseInt((value));
}// end ordinal

 
The way you call this function may affect the results you get, too. Here's the same function with some revisions.
Code:
function search()
{
var thisform = document.forms['form name here'] 
var startdate = new Date(thisform.elements['datum1'].value).getTime();
var enddate = new Date(thisform.elements['datum2'].value).getTime();
var today = new Date().getTime();

if (today > startdate)
  {
  alert ('Please, enter correct "Date from" !');
  thisform.elements['datum1'].focus();
  return false;
  }
else if (startdate >= enddate)
  {
  alert ('Please, enter correct "Date to" !');
  thisform.elements['datum2'].focus();
  return false;
  }
else
  {
  thisform.action = "rezultati-rez.asp";
  thisform.method = "POST";
  thisform.submit();
  }
}

Of course, this function in no way checks to see if the date formats in the text boxes is valid input, so could (and WILL) easily fail to function correctly under those circumstances.

Lee
 
Thanks both of you, I'll check and see this.
It doesn't have to check date format bacause the only way to enter the value in field is by using popup calendar
 
I find what the problem is ! This calendar I'm using on the page shows date in dd/mm/yyyy format in text field, but when using
Code:
var startdate = new Date(form.datum1.value)
I get mm/dd/yyyy ! How to convert one of these ?


 
You can put this prototype just under your <script> tag.

Code:
String.prototype.switchChars = function() {
   var monthValue = this.substr(3, 3);
   var newString = this.replace(monthValue, "");
   newString = monthValue + newString;
   return newString;
}

To switch up the month and date, just do a call like this:
Code:
var startdate = new Date(form.datum1.value[!]switchChars()[/!])











[small]"I see pretty girls everywhere I look, everywhere I look, everywhere I look. - Band song on movie "The Ringer"[/small]
<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top