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

17,620

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;
    }   
});
Share:
17,620
user2203633
Author by

user2203633

Updated on July 19, 2022

Comments

  • user2203633
    user2203633 almost 2 years

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