简体   繁体   中英

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 , ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM