oxc-project/oxc · error · OxcDiagnostic
Prefer `Date.now()` over `new Date().{bad_method}()`
Error message
Prefer `Date.now()` over `new Date().{bad_method}()` What it means
Diagnostic from the unicorn/prefer-date-now lint rule. It fires when code calls a timestamp-returning method (e.g. getTime(), valueOf()) or otherwise coerces a freshly constructed `new Date()` with no arguments, instead of calling `Date.now()` directly. `new Date()` allocates a Date object only to read the current epoch time; `Date.now()` returns the same milliseconds since the Unix epoch without the allocation. It is a style/performance suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:19
use oxc_ast::{
AstKind,
ast::{Argument, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
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()`.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `new Date().getTime()` with `Date.now()`
- Replace `new Date().valueOf()` with `Date.now()`
- Apply the rule's auto-fix, which rewrites the expression to `Date.now()`
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:19 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/d77e0d3ea21975df.
Report an issue: GitHub.