useDateNow
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/complexity/useDateNow¥Diagnostic Category:
lint/complexity/useDateNow -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
¥Same as
unicorn/prefer-date-now
-
¥How to configure
{ "linter": { "rules": { "complexity": { "useDateNow": "error" } } }}¥Description
使用 Date.now() 获取自 Unix 纪元以来的毫秒数。
¥Use Date.now() to get the number of milliseconds since the Unix Epoch.
Date.now() 比 new Date().getTime() 及其变体更具可读性,它还避免了不必要的 Date 对象实例化。
¥Date.now() is more readable than new Date().getTime() and its variants,
it also avoids unnecessary instantiation of Date object.
¥Examples
¥Invalid
const foo = new Date().getTime();code-block.js:1:13 lint/complexity/useDateNow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Date.now() instead of new Date().getTime().
> 1 │ const foo = new Date().getTime();
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Date.now() is more readable and also avoids unnecessary instantiation of Date object.
ℹ Unsafe fix: Replace with Date.now().
1 │ - const·foo·=·new·Date().getTime();
1 │ + const·foo·=·Date.now();
2 2 │
const foo = new Date().valueOf();code-block.js:1:13 lint/complexity/useDateNow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Date.now() instead of new Date().valueOf().
> 1 │ const foo = new Date().valueOf();
│ ^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Date.now() is more readable and also avoids unnecessary instantiation of Date object.
ℹ Unsafe fix: Replace with Date.now().
1 │ - const·foo·=·new·Date().valueOf();
1 │ + const·foo·=·Date.now();
2 2 │
const foo = +new Date();code-block.js:1:13 lint/complexity/useDateNow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Date.now() instead of new Date().
> 1 │ const foo = +new Date();
│ ^^^^^^^^^^^
2 │
ℹ Date.now() is more readable and also avoids unnecessary instantiation of Date object.
ℹ Unsafe fix: Replace with Date.now().
1 │ - const·foo·=·+new·Date();
1 │ + const·foo·=·Date.now();
2 2 │
const foo = Number(new Date());code-block.js:1:13 lint/complexity/useDateNow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Date.now() instead of Number(new Date()).
> 1 │ const foo = Number(new Date());
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Date.now() is more readable and also avoids unnecessary instantiation of Date object.
ℹ Unsafe fix: Replace with Date.now().
1 │ - const·foo·=·Number(new·Date());
1 │ + const·foo·=·Date.now();
2 2 │
const foo = new Date() * 2;code-block.js:1:13 lint/complexity/useDateNow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Date.now() instead of new Date().
> 1 │ const foo = new Date() * 2;
│ ^^^^^^^^^^
2 │
ℹ Date.now() is more readable and also avoids unnecessary instantiation of Date object.
ℹ Unsafe fix: Replace with Date.now().
1 │ - const·foo·=·new·Date()·*·2;
1 │ + const·foo·=·Date.now()*·2;
2 2 │
¥Valid
const foo = Date.now();const foo = Date.now() * 2;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号