AphrodyBun GitHub

Runtime docs · Linter · Rules

unicorn/consistent-date-clone

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

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

Reported:

const deadline = new Date(start.getTime());
deadline.setDate(deadline.getDate() + 7);

Not reported:

const deadline = new Date(start);
deadline.setDate(deadline.getDate() + 7);

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

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