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

      What I Wish Someone Told Me When I Was Getting Into ARIA

      June 17, 2025

      SD Times 100

      June 17, 2025

      Managing the growing risk profile of agentic AI and MCP in the enterprise

      June 17, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 16, 2025

      Funny Windows 11 bug brings back classic Windows boot sound from 20 years ago

      June 17, 2025

      Windows 11 news and updates in June: Microsoft’s AI agent in Settings makes adjusting your PC easier than ever

      June 17, 2025

      uBlock Origin ships to Edge for Android as Google kills it on Chrome

      June 17, 2025

      Windows Hello face unlock no longer works in the dark, and Microsoft says it’s not a bug

      June 17, 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

      Community News: Latest PECL Releases (06.17.2025)

      June 17, 2025
      Recent

      Community News: Latest PECL Releases (06.17.2025)

      June 17, 2025

      Stream-Omni: Simultaneous Multimodal Interactions with Large Language-Vision-Speech Model

      June 17, 2025

      How Inclusive Design Leading and Creating Solutions for Universal Design

      June 17, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Funny Windows 11 bug brings back classic Windows boot sound from 20 years ago

      June 17, 2025
      Recent

      Funny Windows 11 bug brings back classic Windows boot sound from 20 years ago

      June 17, 2025

      Windows 11 news and updates in June: Microsoft’s AI agent in Settings makes adjusting your PC easier than ever

      June 17, 2025

      uBlock Origin ships to Edge for Android as Google kills it on Chrome

      June 17, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: A Second Date

    CodeSOD: A Second Date

    June 17, 2025

    Ah, bad date handling. We’ve all seen it. We all know it. So when Lorenzo sent us this C# function, we almost ignored it:

    private string GetTimeStamp(DateTime param)
    {
        string retDate = param.Year.ToString() + "-";
        if (param.Month < 10)
            retDate = retDate + "0" + param.Month.ToString() + "-";
        else
            retDate = retDate + param.Month.ToString() + "-";
    
        if (param.Day < 10)
            retDate = retDate + "0" + param.Day.ToString() + " ";
        else
            retDate = retDate + param.Day.ToString() + " ";
    
        if (param.Hour < 10)
            retDate = retDate + "0" + param.Hour.ToString() + ":";
        else
            retDate = retDate + param.Hour.ToString() + ":";
    
        if (param.Minute < 10)
            retDate = retDate + "0" + param.Minute.ToString() + ":";
        else
            retDate = retDate + param.Minute.ToString() + ":";
    
        if (param.Second < 10)
            retDate = retDate + "0" + param.Second.ToString() + ".";
        else
            retDate = retDate + param.Second.ToString() + ".";
    
        if (param.Millisecond < 10)
            retDate = retDate + "0" + param.Millisecond.ToString();
        else
            retDate = retDate + param.Millisecond.ToString();
    
        return retDate;
    }
    

    Most of this function isn’t terribly exciting. We’ve seen this kind of bad code before, but even when we see a repeat like this, there are still special treats in it. Look at the section for handling milliseconds: if the number is less than 10, they pad it with a leading zero. Just the one, though. One leading zero should be enough for everybody.

    But that’s not the thing that makes this code special. You see, there’s another function worth looking at:

    private string FileTimeStamp(DateTime param)
    {
        string retDate = param.Year.ToString() + "-";
        if (param.Month < 10)
            retDate = retDate + "0" + param.Month.ToString() + "-";
        else
            retDate = retDate + param.Month.ToString() + "-";
    
        if (param.Day < 10)
            retDate = retDate + "0" + param.Day.ToString() + " ";
        else
            retDate = retDate + param.Day.ToString() + " ";
    
        if (param.Hour < 10)
            retDate = retDate + "0" + param.Hour.ToString() + ":";
        else
            retDate = retDate + param.Hour.ToString() + ":";
    
        if (param.Minute < 10)
            retDate = retDate + "0" + param.Minute.ToString() + ":";
        else
            retDate = retDate + param.Minute.ToString() + ":";
    
        if (param.Second < 10)
            retDate = retDate + "0" + param.Second.ToString() + ".";
        else
            retDate = retDate + param.Second.ToString() + ".";
    
        if (param.Millisecond < 10)
            retDate = retDate + "0" + param.Millisecond.ToString();
        else
            retDate = retDate + param.Millisecond.ToString();
    
        return retDate;
    }
    

    Not only did they fail to learn the built-in functions for formatting dates, they forgot about the functions they wrote for formatting dates, and just wrote (or realistically, copy/pasted?) the same function twice.

    At least both versions have the same bug with milliseconds. I don’t know if I could handle it if they were inconsistent about that.

    [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.

    Source: Read MoreÂ

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleTeam46 (TaxOff) Exploits Google Chrome Zero-Day (CVE-2025-2783) in Sophisticated Phishing Campaign
    Next Article Haiku OS: Sistema Operativo Libero e Innovativo Avanza

    Related Posts

    News & Updates

    Funny Windows 11 bug brings back classic Windows boot sound from 20 years ago

    June 17, 2025
    News & Updates

    Windows 11 news and updates in June: Microsoft’s AI agent in Settings makes adjusting your PC easier than ever

    June 17, 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

    Cardo is a GUI podcast client written in TypeScript

    Linux

    CVE-2025-47240 – Apache Unverified Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Some of my favorite FPS games are getting big remasters next week, and you’ll get them free if you own the originals

    News & Updates

    CVE-2024-9993 – “Elementor Addons for WordPress Stored Cross-Site Scripting Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    News & Updates

    The next Marvel video game has been delayed into the prime GTA 6 timeline

    May 15, 2025

    Originally slated for a 2025 release, Marvel 1943: Rise of Hydra, a game we still…

    CVE-2025-4194 – WordPress AlT Monitoring CSRF

    May 17, 2025

    A Growth Agency that Automate and Scales Businesses

    May 2, 2025

    CVE-2025-4505 – A vulnerability was found in PHPGurukul Apartment

    May 10, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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