javascript-prefer-date-now-over-new-date-gettime
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
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
Datebindings, such asconst 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
becomes:
js
Development
bash
License
MIT