unicorn/prefer-date-now Pedantic
它的作用
优先使用 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 中添加。
