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

      CodeSOD: Functionally, a Date

      September 16, 2025

      Creating Elastic And Bounce Effects With Expressive Animator

      September 16, 2025

      Microsoft shares Insiders preview of Visual Studio 2026

      September 16, 2025

      From Data To Decisions: UX Strategies For Real-Time Dashboards

      September 13, 2025

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 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

      Can I use React Server Components (RSCs) today?

      September 16, 2025
      Recent

      Can I use React Server Components (RSCs) today?

      September 16, 2025

      Perficient Named among Notable Providers in Forrester’s Q3 2025 Commerce Services Landscape

      September 16, 2025

      Sarah McDowell Helps Clients Build a Strong AI Foundation Through Salesforce

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

      I Ran Local LLMs on My Android Phone

      September 16, 2025
      Recent

      I Ran Local LLMs on My Android Phone

      September 16, 2025

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      sudo vs sudo-rs: What You Need to Know About the Rust Takeover of Classic Sudo Command

      September 14, 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:

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-built_in">string</span> <span class="hljs-title">GetTimeStamp</span>(<span class="hljs-params">DateTime param</span>)</span>
    {
        <span class="hljs-built_in">string</span> retDate = param.Year.ToString() + <span class="hljs-string">"-"</span>;
        <span class="hljs-keyword">if</span> (param.Month < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Month.ToString() + <span class="hljs-string">"-"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Month.ToString() + <span class="hljs-string">"-"</span>;
    
        <span class="hljs-keyword">if</span> (param.Day < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Day.ToString() + <span class="hljs-string">" "</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Day.ToString() + <span class="hljs-string">" "</span>;
    
        <span class="hljs-keyword">if</span> (param.Hour < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Hour.ToString() + <span class="hljs-string">":"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Hour.ToString() + <span class="hljs-string">":"</span>;
    
        <span class="hljs-keyword">if</span> (param.Minute < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Minute.ToString() + <span class="hljs-string">":"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Minute.ToString() + <span class="hljs-string">":"</span>;
    
        <span class="hljs-keyword">if</span> (param.Second < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Second.ToString() + <span class="hljs-string">"."</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Second.ToString() + <span class="hljs-string">"."</span>;
    
        <span class="hljs-keyword">if</span> (param.Millisecond < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Millisecond.ToString();
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Millisecond.ToString();
    
        <span class="hljs-keyword">return</span> 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:

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-built_in">string</span> <span class="hljs-title">FileTimeStamp</span>(<span class="hljs-params">DateTime param</span>)</span>
    {
        <span class="hljs-built_in">string</span> retDate = param.Year.ToString() + <span class="hljs-string">"-"</span>;
        <span class="hljs-keyword">if</span> (param.Month < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Month.ToString() + <span class="hljs-string">"-"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Month.ToString() + <span class="hljs-string">"-"</span>;
    
        <span class="hljs-keyword">if</span> (param.Day < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Day.ToString() + <span class="hljs-string">" "</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Day.ToString() + <span class="hljs-string">" "</span>;
    
        <span class="hljs-keyword">if</span> (param.Hour < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Hour.ToString() + <span class="hljs-string">":"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Hour.ToString() + <span class="hljs-string">":"</span>;
    
        <span class="hljs-keyword">if</span> (param.Minute < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Minute.ToString() + <span class="hljs-string">":"</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Minute.ToString() + <span class="hljs-string">":"</span>;
    
        <span class="hljs-keyword">if</span> (param.Second < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Second.ToString() + <span class="hljs-string">"."</span>;
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Second.ToString() + <span class="hljs-string">"."</span>;
    
        <span class="hljs-keyword">if</span> (param.Millisecond < <span class="hljs-number">10</span>)
            retDate = retDate + <span class="hljs-string">"0"</span> + param.Millisecond.ToString();
        <span class="hljs-keyword">else</span>
            retDate = retDate + param.Millisecond.ToString();
    
        <span class="hljs-keyword">return</span> 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

    DistroWatch Weekly, Issue 1139

    September 14, 2025
    News & Updates

    Building personal apps with open source and AI

    September 12, 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

    CodeSOD: Born Single

    News & Updates

    Microsoft rolls out AI actions in File Explorer to more Insiders on Windows 11 as Canary Channel continues to play catch up

    News & Updates

    From drop-out to software architect with Jason Lengstorf [Podcast #167]

    Development

    Meet ReSearch: A Novel AI Framework that Trains LLMs to Reason with Search via Reinforcement Learning without Using Any Supervised Data on Reasoning Steps

    Machine Learning

    Highlights

    Development

    The Hidden Threat in Your Stack: Why Non-Human Identity Management is the Next Cybersecurity Frontier

    June 10, 2025

    Modern enterprise networks are highly complex environments that rely on hundreds of apps and infrastructure…

    CVE-2025-6718 – B1.lt WordPress SQL Injection

    July 18, 2025

    CVE-2025-54581 – Vproxy HTTP Proxy-Authorization Header DoS Vulnerability

    July 30, 2025

    This Rubbish Icon Might Get a Makeover in Ubuntu 25.10

    June 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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