Skip to content

dateConstructorClones

Prefer passing a Date directly to the Date constructor when cloning, rather than calling getTime().

✅ This rule is included in the ts logical preset.

The Date constructor can clone a Date object directly when passed as an argument. Calling .getTime() first is unnecessary since ES2015.

Before ES2015, new Date(date) converted the date to a string first, making it unreliable for cloning. Modern JavaScript runtimes pass the date value directly, making the .getTime() call redundant.

const original = new Date();
const clone = new Date(original.getTime());
function cloneDate(date: Date) {
return new Date(date.getTime());
}

This rule is not configurable.

If your codebase needs to support pre-ES2015 environments where new Date(date) behavior was unreliable, or if you have manually changed global aspects of Date such as the .getTime() method, you may need to disable this rule. However, most modern codebases target ES2015 or later and can use the direct approach.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.