unicorn/consistent-date-clone

Require new Date(date) instead of new Date(date.getTime()) to copy a date

fixable

The Date constructor accepts another Date and copies it exactly. Converting it to a number first with .getTime() adds a call and gives the same result.

--fix removes the .getTime() call.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/consistent-date-clone" = "error" # or "warn"

Examples#

example.js1 problem
const deadline = new Date(start.getTime());
deadline.setDate(deadline.getDate() + 7);
example.jsNo problems
const deadline = new Date(start);
deadline.setDate(deadline.getDate() + 7);

Arithmetic on the timestamp isn't a plain copy, so it's allowed:

example.jsNo problems
const deadline = new Date(start.getTime() + 60_000);