oxc-project/oxc · error · OxcDiagnostic
Prefer `Date.now()` over `{kind}(new Date())`
Error message
Prefer `Date.now()` over `{kind}(new Date())` What it means
Diagnostic from the unicorn/prefer-date-now lint rule. It fires when `new Date()` is passed through a numeric operation or coercion, such as `Math.abs(new Date())`, `Math.floor(+new Date())`, or binary arithmetic on `new Date()`, to obtain the current epoch time. The result is identical to `Date.now()` but goes through object allocation and implicit coercion. It is a lint warning, not a runtime failure.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:25
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};
use crate::{AstNode, context::LintContext, rule::Rule};
fn prefer_date_now(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `Date.now()` over `new Date()`")
.with_help("Change to `Date.now()`.")
.with_label(span)
}
fn prefer_date_now_over_methods(span: Span, bad_method: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Prefer `Date.now()` over `new Date().{bad_method}()`"))
.with_help("Change to `Date.now()`.")
.with_label(span)
}
fn prefer_date_now_over_number_date_object(span: Span, kind: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Prefer `Date.now()` over `{kind}(new Date())`"))
.with_help("Change to `Date.now()`.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferDateNow;
declare_oxc_lint!(
/// ### What it does
///
/// Prefers use of `Date.now()` over `new Date().getTime()` or `new Date().valueOf()`.
///
/// ### Why is this bad?
///
/// Using `Date.now()` is shorter and nicer than `new Date().getTime()`, and avoids unnecessary instantiation of `Date` objects.
///
/// ### Examples
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace the numeric coercion of `new Date()` with a direct `Date.now()` call
- Simplify `Math.floor(+new Date())` to `Date.now()` (epoch ms is already an integer)
- Apply the rule's auto-fix where available
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:25 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/4b87e4d99ed1f606.
Report an issue: GitHub.