Skip to content

noctcore-code-quality/no-bare-date-now

Read wall-clock time through a mockable clock util, not bare Date.now() / new Date().

Recommended preset: error · Autofix: no · Suggestions: no · Type information: not needed

Business logic that reads Date.now() or new Date() directly is hard to test: every time-dependent branch depends on the real clock. Routing wall-clock reads through a shared clock util (nowMs() / now()) gives time-dependent code one seam you can freeze or advance in tests.

  • Date.now() → suggests nowMs().
  • Zero-argument new Date() → suggests now().
Incorrect · 2 reports
// bare clock reads in business logic
const start = Date.now();
const created = new Date();
Correct
import { now, nowMs } from './clock';
// through the clock seam
const start = nowMs();
const created = now();
// parsing an explicit instant
const at = new Date('2026-01-01T00:00:00Z');
  • new Date(value) with an argument: that is a parse of an explicit instant, not a bare clock read.
  • Files covered by allowIn, which by default is the clock util itself (**/clock.ts, **/clock/**).
Option Type Default Meaning
allowIn string[] (globs) ["**/clock.ts", "**/clock/**"] File-path globs (infra / the clock util itself) where bare Date is allowed.
'noctcore-code-quality/no-bare-date-now': ['error', { allowIn: ['**/clock.ts', '**/*.timing.ts'] }]

If your project has no clock abstraction and does not intend to add one.