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
Solved

Help with javascript function

Posted on 2013-11-14
3
209 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

856 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