我有这个功能:

   isAuthenticationExpired = (expirationDate: Date) => {
        var now = new Date();
        if (expirationDate - now > 0) {
            return false;
        } else {
            return true;
        }
    }
expirationDatenow均为日期类型

typescript 给我一个错误,说:
Error   3   The left-hand side of an arithmetic operation must be of type
'any', 'number' or an enum type.

Error   4   The right-hand side of an arithmetic operation must be of type
'any', 'number' or an enum type.

由于我的方式似乎不起作用,如何检查日期是否已过期?

最佳答案

使用now获取日期expirationDate.valueOf()的整数值表示形式(自unix纪元以来的毫秒数)

var now = new Date().valueOf();
expirationDate = expirationDate.valueOf();

或者,使用Date.now()

10-06 03:38