src/content/docs/linter/rules/use-date-now.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.8.0` - Diagnostic Category: [`lint/complexity/useDateNow`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`unicorn/prefer-date-now`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-date-now.md) - Same as [`e18e/prefer-date-now`](https://github.com/e18e/eslint-plugin){
"linter": {
"rules": {
"complexity": {
"useDateNow": "error"
}
}
}
}
Use Date.now() to get the number of milliseconds since the Unix Epoch.
Date.now() is more readable than new Date().getTime() and its variants,
it also avoids unnecessary instantiation of Date object.
const foo = new Date().getTime();
const foo = new Date().valueOf();
const foo = +new Date();
const foo = Number(new Date());
const foo = new Date() * 2;
const foo = Date.now();
const foo = Date.now() * 2;