{"record":{"id":"d77e0d3ea21975df","repo":"oxc-project/oxc","slug":"prefer-date-now-over-new-date-bad-method","errorCode":null,"errorMessage":"Prefer `Date.now()` over `new Date().{bad_method}()`","messagePattern":"Prefer `Date\\.now\\(\\)` over `new Date\\(\\)\\.(.+?)\\(\\)`","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs","lineNumber":19,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_date_now(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `Date.now()` over `new Date()`\")\n        .with_help(\"Change to `Date.now()`.\")\n        .with_label(span)\n}\n\nfn prefer_date_now_over_methods(span: Span, bad_method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `Date.now()` over `new Date().{bad_method}()`\"))\n        .with_help(\"Change to `Date.now()`.\")\n        .with_label(span)\n}\n\nfn prefer_date_now_over_number_date_object(span: Span, kind: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `Date.now()` over `{kind}(new Date())`\"))\n        .with_help(\"Change to `Date.now()`.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferDateNow;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers use of `Date.now()` over `new Date().getTime()` or `new Date().valueOf()`.\n    ///","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs#L1-L37","documentation":"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.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_date_now.rs:19 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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()`"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}