Solved

Help with javascript function

Posted on 2013-11-14
3
192 Views
Last Modified: 2013-11-21
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

0
Comment
Question by:J C
3 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39650410
Something like this should do the trick:
function VerifyOnTime(actualEndDate, plannedEndDate) {
    var res = "NONE";
    var today = (new Date()).getTime();
    var ped = Date.parse(plannedEndDate);
    var aed = (actualEndDate && actualEndDate != '') ? Date.parse(actualEndDate) : null;

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

    return res;
}

Open in new window


Here's a working sample: http://jsfiddle.net/aLkZd/
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39650424
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

0
 

Author Closing Comment

by:J C
ID: 39666169
This worked perfectly. Thanks a lot!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now