This rule raises an issue when code creates a new Date object just to get the current timestamp as a number.
Creating a Date object and then immediately converting it to a number is unnecessarily verbose and inefficient. JavaScript provides
Date.now() as a direct way to get the current timestamp.
When you write new Date().getTime() or similar patterns, you are:
The Date.now() method was specifically designed for this purpose. It returns the number of milliseconds since January 1, 1970 UTC,
which is exactly what these other patterns achieve but in a more direct way.
Patterns like +new Date() or Number(new Date()) rely on JavaScript’s type coercion, which can be confusing for other
developers reading the code. These implicit conversions make the code harder to understand at first glance.
This issue affects code readability and performance. While the performance impact is minimal in most cases, using Date.now() is the
standard, recommended approach that clearly expresses the intent to get the current timestamp.
Replace new Date().getTime() with Date.now() to get the current timestamp more efficiently.
const timestamp = new Date().getTime(); // Noncompliant
const timestamp = Date.now();