如何使用Typescript检查Date数据类型变量是否过去?

Sam*_*tar 2 javascript typescript

我有这个功能:

   isAuthenticationExpired = (expirationDate: Date) => {
        var now = new Date();
        if (expirationDate - now > 0) {
            return false;
        } else {
            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这两个expirationDatenow的类型日期

打字稿给我一个错误说:

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.    
Run Code Online (Sandbox Code Playgroud)

我怎样才能检查日期是否已过期,因为我的方式似乎不起作用?

Pau*_* S. 5

获取Date的日期和使用的整数值表示(自unix时期以来为ms) nowexpirationDate.valueOf()

var now = new Date().valueOf();
expirationDate = expirationDate.valueOf();
Run Code Online (Sandbox Code Playgroud)

或者,使用 Date.now()