Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: Across the 4th Dimension

      September 25, 2025

      Cursor vs GitHub Copilot (2025): Which AI Platform Wins for Your Node.js Dev Team?

      September 25, 2025

      NuGet adds support for Trusted Publishing

      September 25, 2025

      AWS launches IDE extension for building browser automation agents

      September 25, 2025

      Distribution Release: Kali Linux 2025.3

      September 23, 2025

      Distribution Release: SysLinuxOS 13

      September 23, 2025

      Development Release: MX Linux 25 Beta 1

      September 22, 2025

      DistroWatch Weekly, Issue 1140

      September 21, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Beyond Denial: How AI Concierge Services Can Transform Healthcare from Reactive to Proactive

      September 25, 2025
      Recent

      Beyond Denial: How AI Concierge Services Can Transform Healthcare from Reactive to Proactive

      September 25, 2025

      IDC ServiceScape for Microsoft Power Apps Low-Code/No-Code Custom Application Development Services

      September 25, 2025

      A Stream-Oriented UI library for interactive web applications

      September 24, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      FOSS Weekly #25.39: Kill Switch Phones, LMDE 7, Zorin OS 18 Beta, Polybar, Apt History and More Linux Stuff

      September 25, 2025
      Recent

      FOSS Weekly #25.39: Kill Switch Phones, LMDE 7, Zorin OS 18 Beta, Polybar, Apt History and More Linux Stuff

      September 25, 2025

      Distribution Release: Kali Linux 2025.3

      September 23, 2025

      Distribution Release: SysLinuxOS 13

      September 23, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: A Double Date

    CodeSOD: A Double Date

    May 5, 2025

    Alice picked up a ticket about a broken date calculation in a React application, and dropped into the code to take a look. There, she found this:

    <span class="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">calcYears</span>(<span class="hljs-params">date</span>) {
      <span class="hljs-keyword">return</span> date && <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">floor</span>((<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>() - <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(date).<span class="hljs-title function_">getTime</span>()) / <span class="hljs-number">3.15576e10</span>)
    }
    

    She stared at it for awhile, trying to understand what the hell this was doing, and why it was dividing by three billion. Also, why there was a && in there. But after staring at it for a few minutes, the sick logic of the code makes sense. getTime returns a timestamp in milliseconds. 3.15576e10 is the number of milliseconds in a year. So the Math.floor() expression just gets the difference between two dates as a number of years. The && is just a coalescing operator- the last truthy value gets returned, so if for some reason we can’t calculate the number of years (because of bad input, perhaps?), we just return the original input date, because that’s a brillant way to handle errors.

    As bizarre as this code is, this isn’t the code that was causing problems. It works just fine. So why did Alice get a ticket? She spent some more time puzzling over that, while reading through the code, only to discover that this calcYears function was used almost everywhere in the code- but in one spot, someone decided to write their own.

    <span class="hljs-keyword">if</span> (birthday) {
          <span class="hljs-keyword">let</span> year = birthday?.<span class="hljs-title function_">split</span>(<span class="hljs-string">'-'</span>, <span class="hljs-number">1</span>)
          <span class="hljs-keyword">if</span> (year[<span class="hljs-number">0</span>] != <span class="hljs-string">''</span>) {
            <span class="hljs-keyword">let</span> years = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>().<span class="hljs-title function_">getFullYear</span>() - year[<span class="hljs-number">0</span>]
            <span class="hljs-keyword">return</span> years
          }
    }
    

    So, this function also works, and is maybe a bit more clear about what it’s doing than the calcYears. But note the use of split– this assumes a lot about the input format of the date, and that assumption isn’t always reliable. While calcYears still does unexpected things if you fail to give it good input, its accepted range of inputs is broader. Here, if we’re not in a date format which starts with “YYYY-“, this blows up.

    After spending hours puzzling over this, Alice writes:

    I HATE HOW NO ONE KNOWS HOW TO CODE

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleIranian APT Group Breaches Middle Eastern Critical Infrastructure in Stealth Campaign
    Next Article CVE-2025-4269 – TOTOLINK Log Handler Remote Code Execution Vulnerability

    Related Posts

    News & Updates

    Distribution Release: Kali Linux 2025.3

    September 23, 2025
    News & Updates

    Distribution Release: SysLinuxOS 13

    September 23, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-8756 – TDuckCloud Authorization Interceptor PreHandle Remote Improper Authorization Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Storm-2603 Deploys DNS-Controlled Backdoor in Warlock and LockBit Ransomware Attacks

    Development

    CVE-2025-31947 – Mattermost LDAP Account Lock Bypass Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4122 – Netgear JWNR2000 Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Site Update – An Apology and a Brighter Future

    April 6, 2025

    We’ve spent time optimizing the site including migrating to the latest server technology. This provides…

    Postgres RAG Stack: Embedding, Chunking & Vector Search

    July 17, 2025

    CVE-2025-33097 – IBM QRadar SIEM Cross-Site Scripting Vulnerability

    July 15, 2025

    From Failure to International Success: How Online Learning Platforms Saved My Life

    April 2, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.