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

      The Power Of The Intl API: A Definitive Guide To Browser-Native Internationalization

      August 8, 2025

      This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)

      August 8, 2025

      Elastic simplifies log analytics for SREs and developers with launch of Log Essentials

      August 7, 2025

      OpenAI launches GPT-5

      August 7, 2025

      3 portable power stations I travel everywhere with (and how they differ)

      August 9, 2025

      I tried Lenovo’s new rollable ThinkBook and can’t go back to regular-sized screens

      August 9, 2025

      The Creators of the Acclaimed Silent Hill 2 Remake Present a Deep Dive Into the Story of Their Newest Horror Game IP — and It’s So Bizarre and Insane That It’s Convinced Me To Put It on My Wishlist

      August 9, 2025

      Forget Back to School Deals — Lenovo’s Clearance Sale is Where You’ll Find Amazing Discounts on Laptops, Mini PCs, and More, While Supplies Last

      August 9, 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

      spatie/laravel-flare

      August 9, 2025
      Recent

      spatie/laravel-flare

      August 9, 2025

      Establishing Consistent Data Foundations with Laravel’s Database Population System

      August 8, 2025

      Generate Postman Collections from Laravel Routes

      August 8, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      The Creators of the Acclaimed Silent Hill 2 Remake Present a Deep Dive Into the Story of Their Newest Horror Game IP — and It’s So Bizarre and Insane That It’s Convinced Me To Put It on My Wishlist

      August 9, 2025
      Recent

      The Creators of the Acclaimed Silent Hill 2 Remake Present a Deep Dive Into the Story of Their Newest Horror Game IP — and It’s So Bizarre and Insane That It’s Convinced Me To Put It on My Wishlist

      August 9, 2025

      Forget Back to School Deals — Lenovo’s Clearance Sale is Where You’ll Find Amazing Discounts on Laptops, Mini PCs, and More, While Supplies Last

      August 9, 2025

      The Gaming Desktop I’ve Relied on More Than Any Other Is More Powerful and Sleeker Than Ever — But Damn, It’s Expensive

      August 9, 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:

    export function calcYears(date) {
      return date && Math.floor((new Date() - new Date(date).getTime()) / 3.15576e10)
    }
    

    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.

    if (birthday) {
          let year = birthday?.split('-', 1)
          if (year[0] != '') {
            let years = new Date().getFullYear() - year[0]
            return 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

    3 portable power stations I travel everywhere with (and how they differ)

    August 9, 2025
    News & Updates

    I tried Lenovo’s new rollable ThinkBook and can’t go back to regular-sized screens

    August 9, 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

    CTEM is the New SOC: Shifting from Monitoring Alerts to Measuring Risk

    Development

    Embed an AI operator into your SaaS with just one line of code.

    Web Development

    Citrix NetScaler ADC and Gateway Vulnerability Actively Exploited in the Wild

    Security

    CVE-2025-5832 – Pioneer DMH-WT7600NEX Code Execution Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Development

    Enhance Email Validation with Laravel’s Fluent Email Rule Object

    May 22, 2025

    Laravel’s Email rule object transforms email validation with a fluent interface that combines format checking,…

    CVE-2025-4163 – PHPGurukul Land Record System SQL Injection Vulnerability

    May 1, 2025

    Linux Candy: walrs – fast colorscheme generator

    May 16, 2025

    Meet GenSpark Super Agent: The All-in-One AI Agent that Autonomously Think, Plan, Act, and Use Tools to Handle All Your Everyday Tasks

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

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