Skip to content

noUselessDateGetTime (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessDateGetTime": "error"
}
}
}
}

Disallow calling .getTime() on a date before passing it to the Date constructor.

Passing a Date object to new Date() creates a copy of it. Calling .getTime() on the date first does the same thing with extra code.

const copy = new Date(date.getTime());
code-block.js:1:28 lint/nursery/noUselessDateGetTime  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This .getTime() call is unnecessary.

> 1 │ const copy = new Date(date.getTime());
│ ^^^^^^^^^
2 │

ℹ Passing a date directly to new Date() already creates a copy of it.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the .getTime() call.

1 │ - const·copy·=·new·Date(date.getTime());
1 │ + const·copy·=·new·Date(date);
2 2 │

const copy = new Date(date);
const timestamp = date.getTime();