Link to home
Start Free TrialLog in
Avatar of J C
J CFlag for United States of America

asked on

Help with javascript function

I am having a hard time getting this function to work correctly. I am new to javascript and am not sure where my error is.

What I am trying to accomplish:
Logic
If the actualenddate does not exist and the plannedenddate is in the future res=None

if the actualenddate does exist and it is greater than the plannedenddate res=Behind

if the actualenddate does not exist and the plannedenddate is < today res=Delinquent

if the actualenddate does exist and is <= plannedenddate res=Ahead


function VerifyOnTime(actualEndDate,plannedEndDate){
	//alert("actual: "+actualEndDate+" planned: "+plannedEndDate);
	
	var res="NONE";
	var ped=Date.parse(plannedEndDate);
	
	var today=(new Date()).getTime();
	var aed=null;
	if (actualEndDate!='')
		aed=Date.parse(actualEndDate);
	else 
		aed=today;

	
	if (aed!='' && aed>ped){
		res='BEHIND';
	}else if (ped>=today && aed==''){
		res='NONE';
	}else if (aed=='' && ped<today){
		//alert(actualEndDate);
		//alert(ped);
		//alert(today);
		res='DELINQUENT';
	}else if (aed<=ped){
		//alert(actualEndDate);
		//alert(ped);
		//alert(today);
		res='AHEAD';
	}else{
		res='NONE';
	}

	//alert(res);
	return res;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

something like this?
Sample:
http://jsfiddle.net/EE_RainerJ/c3zLJ/

function VerifyOnTime(actualEndDate,plannedEndDate){
      var res="NONE";
      var ped=Date.parse(plannedEndDate);
      
      var today=(new Date()).getTime();
      var aed;
      if (actualEndDate=='')
	  {
		if(ped>today) {
			return res;
		} else {
			return 'DELINQUENT';
		}
	  }
	  else {
		aed=Date.parse(actualEndDate);
		if (aed > ped) {
			return 'BEHIND';
        } else {
			return 'AHEAD';
		}
	}
} 

Open in new window

Avatar of J C

ASKER

This worked perfectly. Thanks a lot!