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 presets.

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
const original: Date
original
= new
var Date: DateConstructor
new () => Date (+4 overloads)
Date
();
const
const clone: Date
clone
= new
var Date: DateConstructor
new (value: number | string | Date) => Date (+4 overloads)
Date
(
const original: Date
original
.
Date.getTime(): number

Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

getTime
());
function
function cloneDate(date: Date): Date
cloneDate
(
date: Date
date
:
interface Date

Enables basic storage and retrieval of dates and times.

Date
) {
return new
var Date: DateConstructor
new (value: number | string | Date) => Date (+4 overloads)
Date
(
date: Date
date
.
Date.getTime(): number

Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

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 ❤️‍🔥 around the world by the Flint team and contributors.