Registry
Mmohebifar avatar

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.

mohebifar0 executions

  • javascript
  • typescript
  • date
  • code-quality
  • readability
Connect a repo
npx codemod javascript-prefer-date-now-over-new-date-gettime

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
# 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();

becomes:

js
const startedAt = Date.now();
const finishedAt = Date.now();

Development

bash
# Test the transformation
pnpm test

# Type-check the codemod
pnpm check-types

# Validate the workflow
pnpm dlx codemod@latest workflow validate -w workflow.yaml

# Publish to registry
pnpm dlx codemod@latest login
pnpm dlx codemod@latest publish

License

MIT

Ready to evolve your codebase?

Build tailored, compiler-aware skills to assess, plan, and automate complex codebase changes.