dateConstructorClones
Prefer passing a
Datedirectly to theDateconstructor when cloning, rather than callinggetTime().
✅ 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.
Examples
Section titled “Examples”const original = new Date();const clone = new Date(original.getTime());function cloneDate(date: Date) { return new Date(date.getTime());}const original = new Date();const clone = new Date(original);function cloneDate(date: Date) { return new Date(date);}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/consistent-date-clone