Solved

Help with javascript function

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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 tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 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

14 Experts available now in Live!

Get 1:1 Help Now