albert albert - 16 days ago 8x
TypeScript Question

Typescript: Date operations From Javascript to typescript

I'm passing a code from Javascript to TypeScript, but I have a problem in a code, where I do an operation with Dates:

function(d) {
let di = new Date(d);
let df = new Date(d);
df.setMonth(d.getMonth() + 1);
let dl = +new Date(+new Date(df) - +new Date(di)) / (1000 * 60 * 60 * 24);
return (dl * cellSize) - 45;

The d variable is a Date object with value 2016-08-31, the problem is the operations in dl: it throws an error:

Argument of type 'Date' is not assignable to parameter of type 'string'.

I have searched on google and StackOverflow, and the only thing I found it was to add a + before new keyword. Is there any other or better way ? Any idea?

Thanks in advance.


di and df are already dates. You're trying to use them as argument to Date, i.e. new Date(new Date). That's a) rather nonsensical and b) where your error comes from (new Date accepts an integer or string as argument, not a Date).

Get rid of the new Date() around df and di.