Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }   

});


        });

    };
like image 795
user2203633 Avatar asked Sep 11 '16 09:09

user2203633


1 Answers

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;
    }   
});
like image 150
Nitzan Tomer Avatar answered Sep 28 '22 15:09

Nitzan Tomer