dateNowTimestamps
Prefer the shorter
Date.now()to get the number of milliseconds since the Unix Epoch.
✅ This rule is included in the ts logical preset.
Date.now() is shorter and more efficient than alternatives like new Date().getTime() because it avoids unnecessary instantiation of Date objects.
This rule reports patterns that create a Date object only to immediately extract the timestamp, such as:
new Date().getTime()new Date().valueOf()+new Date()Number(new Date())
Examples
Section titled “Examples”const timestamp = new Date().getTime();const timestamp = new Date().valueOf();const timestamp = +new Date();const timestamp = Number(new Date());const timestamp = Date.now();const date = new Date();const formattedDate = date.toISOString();Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase intentionally uses new Date().getTime() or similar patterns for stylistic consistency, you may disable this rule.
Some older codebases may prefer the more explicit instantiation pattern.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useDateNow - ESLint:
unicorn/prefer-date-now - Oxlint:
unicorn/prefer-date-now
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.