1

I'm working on an assignment where I must use Javascript. In my application a user types a date into a form. Then I try to compare that date with the current date. The difference is the number of days they have to complete a task. However, I'm having a bit of trouble when it comes to the calculations. dueDate is a paramater for an object dynamically created.

function getFormData() {
    var adate = document.getElementById("dueDate").value;
    if (checkInputText(dueDate, "Please enter a date")) return;    

...
}

function processDate(adate) {
 var splitArray = adate.split("-");
 var year = splitArray[0];
 var month = splitArray[1] - 1;
 var day = splitArray[2];
 var date = new Date(year, month, day);
 var dateParse = date.getTime();
 return dateParse;
}


function compareDates(dueDate) { //dueDate is the value from the form
        var cdate = new Date();
        console.log("this is todays date" + " " + cdate); //shows correct date
        var cdateparse = Date.parse(cdate);
        var dueDateparse = Date.parse(dueDate);
        var diff = dueDateparse - cdateparse;
        var daysCal = diff / 1000 / 60 / 60 / 24;
        var days = parseInt(daysCal); //where I'm having trouble
        console.log(days);
        if (days == 0) {
            mymessage = " you have to do this task today";
        }
        try {
            if (days < 0) {
                mymessage = "this task is overdue by" + " " + -days + " " + "days";
                throw new Error("you are overdue");
            }
        } catch (ex) {
            alert(ex.message);
            return;
        }
        if (days > 0) {
            console.log("the difference is greater than 0");
            mymessage = "you have" + " " + days + " " + "more days";
        }
    }

The issue happens when I put in the current date into the form. I've tried math.floor and math.round but the number is always rounded up and throws my message, saying that the task is overdue. Using parseInt has brought me closest to my desired outcome, but when I put in tomorrow's date it says that I am overdue. Any suggestions?

user2084813
  • 175
  • 2
  • 3
  • 12
  • Can you give an example value for dueDate? I can't tell what value the form gives you from this example. – alexp Mar 19 '13 at 20:02
  • yes, so for today it would be 2013-03-19 – user2084813 Mar 19 '13 at 20:10
  • So what does it say if you try it with todays date? http://jsfiddle.net/mK23N/ -> `you have 1 more days` – dfsq Mar 19 '13 at 20:12
  • I've tried setting a new variable equal to cdate.getTime and then parsing that value and using it in the diff variable but each time I try the message no longer shows up – user2084813 Mar 19 '13 at 20:12
  • with today's date it says "you have to do this task today" which is great, but when I type in tomorrow's date 2013-03-20 it displays the same message. – user2084813 Mar 19 '13 at 20:13
  • Right, because the difference is less than 24 hours – dfsq Mar 19 '13 at 20:15

2 Answers2

2

http://jsfiddle.net/sujesharukil/QspFj/

use

 var days = Math.ceil(daysCal);

instead of parseInt.

Sujesh Arukil
  • 2,469
  • 16
  • 37
0

You should beware of that new Date(year, month, day); creates a timestamp for 0:00 AM.

So everything that happens on that day, will already have a negative diff (> -1, though). So you should use Math.ceil instead of rounding. Or you set the deadline to 23:59:59 (i.e. just increase the day by 1).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375