Reputation: 115
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;
}
});
});
};
Upvotes: 10
Views: 30405
Reputation: 1
When assigning variables in typescript it can help to assign the type explicitly to avoid any confusion.
When assigning:
ts = new Date(2012, 0, 1)
you avoided declaring what type ts was and left it up to the "compiler".
If you had tried:
var ts : number = new Date(2012, 0, 1);
for example (I know this takes up more space, sorry), you would have gotten as similar error.
To get around this, you either need a new variable for your declaration:
ts = new Date(now.getTime() + 24*60*60*1000);
Or more cleanly, you avoid declaring ts and construct:
var note = $('#note')
after you have decided what types all the values will be. Something like:
if(new Date() > (new Date(2012, 0, 1))
and decide from there how your "note" object will be formed.
Upvotes: 0
Reputation: 233
let oneweek = new Date((new Date()).setDate(today.getDate() + 7));
let twoweek = new Date((new Date()).setDate(today.getDate() + 14));
let onemonth = new Date((new Date()).setDate(today.getMonth() + 1));
let threemonth = new Date((new Date()).setDate(today.getMonth() + 3));
Upvotes: -3
Reputation: 164129
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;
}
});
Upvotes: 17