Skip to content
← Back to rules

unicorn/prefer-date-now Pedantic

🛠️ An auto-fix is available for this rule.

它的作用

优先使用 Date.now(),而不是 new Date().getTime()new Date().valueOf()

为什么这不好?

使用 Date.now()new Date().getTime() 更简洁、更优雅,并且避免了不必要的 Date 对象实例化。

示例

以下是此规则的错误代码示例:

javascript
const ts = new Date().getTime();
const ts = new Date().valueOf();

以下是此规则的正确代码示例:

javascript
const ts = Date.now();

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-date-now": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-date-now": "error",
  },
});
bash
oxlint --deny unicorn/prefer-date-now

版本

此规则在 v0.0.16 中添加。

参考资料