如何使用 typescript 获取 unixtime

How to get unixtime with typescript

我在下面试过了。

var unixtime = new Date / 1000;

然后,出现编译错误

error TS2113: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

如何在没有编译错误的情况下获取 unixtime?

使用 + 运算符显式转换为数字:

var x = +new Date / 1000;

或者只使用更快/更清晰的方法Date#now:

var x = Date.now() / 1000;