oxc-project/oxc · error · OxcDiagnostic
Prefer `Date.now()` over `new Date()`
Error message
Prefer `Date.now()` over `new Date()`
What it means
Raised by unicorn/prefer-date-now when `new Date()` is used to obtain the current timestamp. Allocating a Date just for milliseconds is wasteful; the faulting input is the `new Date()` expression at the labeled span, and the fix rewrites it to `Date.now()`.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:13
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;View on GitHub (pinned to e1e7af627c)
Solutions
- Use `Date.now()` which returns the epoch milliseconds directly
- If you need a Date object, keep `new Date()` but derive the timestamp via `date.getTime()`
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:13 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/a3534f3b2e511ea7.
Report an issue: GitHub.