user2203633 user2203633 - 2 months ago 29
jQuery Question

Type 'number' is not assignable to type 'Date' - Typescript not compiling

I've got the following code for a jquery timer plugin. The compiler gives me the error: "Type 'number' is not assignable to type 'Date'"

$(function(){
var note = $('#note'),
ts = new Date(2012, 0, 1),
newYear = false;

if((new Date()) > ts){
ts = (new Date()).getTime() + 24*60*60*1000; //counting 24 hours
newYear = false;
}

});


});

};

Answer

You need to create a new instance of Date:

if((new Date()) > ts){
    ts = new Date((new Date()).getTime() + 24*60*60*1000);
    newYear = false;
}

This way ts is assigned with a new Date with the given time.
Also, there's no need to create two instance of Date for now, you can just put it in a variable an reuse it:

$(function(){
    var note = $('#note'),
        ts = new Date(2012, 0, 1),
        newYear = false,
        now = new Date();

    if(now > ts){
        ts = new Date(now.getTime() + 24*60*60*1000);
        newYear = false;
    }   
});