An Easy Mnemonic to Format Dates As Strings in C#
Cesar Aguirre

Cesar Aguirre @canro91

About: Software engineer, lifelong learner, language enthusiast & avid reader — Get my free 7-day email course to refactor your coding career: bit.ly/csarag-lessons

Location:
Colombia 🇨🇴 (not Columbia)
Joined:
Jul 23, 2019

An Easy Mnemonic to Format Dates As Strings in C#

Publish Date: Apr 2
0 2

I originally posted this post on my blog.


Is it MM or mm for months when formatting dates? Is it "yyyy-MM-dd" or "yyyy-mm-dd"?

I always forgot which one to use...Until I figured out an easy mnemonic.

MM vs mm

Ask yourself: Which one represents a larger time frame? Months or minutes?

Since one month is larger than one minute, it's M for months. An uppercase m is larger than a lowercase m.

Write, anyDate.ToString("yyyy-MM-dd").

mm vs fff

Then, what is it for milliseconds? Isn't it mm?

Now, ask yourself: Which one is faster? One minute or one millisecond?

Since one millisecond is faster, write f.

Write anyTime.ToString("hh:mm:ss.fff").

Easy, peasy now!


Join my email list and get a 2-minute email with curated resources about programming and software engineering every Friday. Don't miss out next Friday email.

Comments 2 total

  • Baltasar García Perez-Schofield
    Baltasar García Perez-SchofieldApr 3, 2025

    Good point.

    Alas, I can't understand how date/time objects don't include a toShortIso() or something like that, producing the data in yyyy-MM-dd/hh:mm:ss formats.

    • Cesar Aguirre
      Cesar AguirreApr 3, 2025

      You're right! The closest methods are ToLongDateString and ToShortDateString...or something like that.

Add comment