Replace verbose current-time patterns like `new Date().getTime()` and `new Date().valueOf()` with `Date.now()` in JavaScript/TypeScript codebases for clearer intent and improved code quality.
Replace verbose current-time patterns like new Date().getTime() and new Date().valueOf() with Date.now() in JavaScript/TypeScript codebases for clearer intent and improved code quality.
Installation
bash
# Run from the registry
npx codemod javascript-prefer-date-now-over-new-date-gettime
# Or run the local package against a target project
pnpm dlx codemod@latest workflow run -w . -t /path/to/project --no-interactive
Usage
This codemod rewrites only immediate zero-argument current-time calls:
new Date().getTime() -> Date.now()
new Date().valueOf() -> Date.now()
(new Date()).getTime() -> Date.now()
(new Date()).valueOf() -> Date.now()
It targets .js, .jsx, .mjs, .cjs, .ts, and .tsx files.
It intentionally does not rewrite these cases:
new Date(...) with any constructor arguments
Calls with arguments, such as new Date().getTime(1)
Shadowed Date bindings, such as const Date = FakeDate
Optional chaining forms such as new Date()?.getTime?.()
Extracted or indirect receivers such as const d = new Date(); d.getTime()
Computed property access such as new Date()['getTime']()
Example
js
const startedAt = new Date().getTime();
const finishedAt = (new Date()).valueOf();