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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking dates is more than 14 before

Status
Not open for further replies.

jeffwest21

IS-IT--Management
Apr 4, 2013
60
GB
Copuld really do with some help with this.

I have been taked with changing someone els's code (never a good thing) and I am stuck

Below is a pice of code that checks if a date is more than 9 months ahead

JavaScript:
	if(checkDD.value.length > 0){
		var d = new Date();
		maxDD = d.setMonth(Today.getMonth() + 9);
		jsMonth = ddmonth.value -1;  //Javascript month values start at 0
		jsDate = new Date(DDyear.value,jsMonth,DDday.value);
		
		if(jsDate > maxDD){
			alert("Please enter a valid due date.");
			return false;
		}
	}

I have created this, which I thought would check if the date is additonally no more than 14 days past

JavaScript:
	if(checkDD.value.length > 0){
		var d = new Date();
		minDD = d.setMonth(d.getDate() - 14);
		jsMonth = ddmonth.value -1;  //Javascript month values start at 0
		jsDate = new Date(DDyear.value,jsMonth,DDday.value);
			
		if(jsDate < minDD){
			alert("Please enter a valid due date, it cannot be more than 14 days ago.");
			return false;
		}
	}

However it doesn't appear to work, the JS file this is in seems to stop at this point and doesn't run a hide command for now required fields, I do not get any error.

I could really do with this clearing this up and i have been stuck on it for a day or so and need to get the website updated.

'Clever boy...'
 
not tested this but something like this should work

Code:
function validateDate( dateObj ){
	var today = new date();
	var uBound = new date();
	uBound.setMonth(today.getMonth() + 9);
	var lBound = new date();
	lBound.setDate(today.getDate() - 14);
	return (dateObj <= uBound && dateObj >= lBound);
}

/*usage */
if(!validateDate( new Date(DDyear.value,DDMonth.value - 1,DDday.value))){
	//date not ok
}
 
Thanks for the quick response, managede to 'bundle' this through in the end by trial and error, however, going to look at this when i get a second and see if this just works better.

'Clever boy...'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top