unicorn/prefer-date-now
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-date-now": "error",
},
},
];Prefer Date.now() to get the number of milliseconds since the Unix Epoch.
const foo = new Date().getTime();const foo = new Date().valueOf();const foo = +new Date;const foo = Date.now();const foo = Date.now() * 2;Examples from the ESLint documentation.
unicorn/prefer-date-now
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-date-now": "error",
},
},
];unicorn/prefer-date-now
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-date-now": "error"
}
}complexity/useDateNow
complexity · since 1.8.0
{
"linter": {
"rules": {
"complexity": {
"useDateNow": "error"
}
}
}
}Prefer BigInt literals over the constructor.
Prefer Math.min() and Math.max() over ternaries for simple comparisons.
Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item.
Prefer Math.trunc() for truncating numbers.
Prefer .flatMap(…) over .map(…).flat() and .filter(…).flatMap(…).
Prefer modern Math APIs over legacy patterns.