Solved

Help with javascript function

Posted on 2013-11-14
3
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

738 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