0

so I just want to find wether a date is before today or a day in future. So basiclly I have this construction:

function data(startdate, enddate)
{
    var that = this;
    start = startdate;
    end = enddate;

    var today = new Date();
    today = today.getTime();
    today = Date.parse(today);

    this.status = false; // True = Server is in maintenance
    this.init = function()
    {
        isDone();
    }
    function isDone(){
        if( Date.parse(start) < today && today < Date.parse(end))
        {       
            that.status = true;
            console.log(that.status);
        }else
        {
            that.status = false;
            console.log(that.status);
        }
    }
}

Than I call it like this:

var x1 = new Date(2013,2,23,12,3,45);
var x2 = new Date(2013,2,27,12,3,45);

var foobar = new data(x1,x2);
foobar.init();

So tried to figure out, what javascript creates:

start:1364036625000 
end: 1364382225000 
today: 1361796935000 

I also parsed it back into the standard format:

start:Sat Mar 23 2013 12:03:45 GMT+0100 
end:Wed Mar 27 2013 12:03:45 GMT+0100 
today:Mon Feb 25 2013 13:54:56 GMT+0100

So I have no idea, how to fix it or how it could work...

4
  • 1
    today = Date.parse(new Date().getTime()); looks horrible. What is it supposed to do? Commented Feb 25, 2013 at 14:30
  • Did you notice that start and end are in March? Commented Feb 25, 2013 at 14:32
  • Its purpose is to get the current time and parse it into seconds. Nope I didn't noticed it :C big failure... Commented Feb 25, 2013 at 14:36
  • I guess you mean milliseconds. And you don't need to do that, it would be done automatically for Date objects when operating mathematically on them (-, *, <, …). And Date.parse expects strings, not Date objects. Commented Feb 25, 2013 at 15:30

1 Answer 1

1

Your problem isn't explicitly stated but you seem to forget (or ignore) that months are zero-indexed in javascript. February is 1.

Sign up to request clarification or add additional context in comments.

1 Comment

Most programmers working with dates in Java or JavaScript have been bitten by this indexing at least once...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.