Skip to content
← Back to rules

unicorn/prefer-date-now Pedantic

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

What it does ​

Prefers use of Date.now() over new Date().getTime() or new Date().valueOf().

Why is this bad? ​

Using Date.now() is shorter and nicer than new Date().getTime(), and avoids unnecessary instantiation of Date objects.

Examples ​

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

javascript
const ts = Date.now();

How to use ​

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

Version ​

This rule was added in v0.0.16.

References ​