DEV Community

Cesar Aguirre
Cesar Aguirre

Posted on • Originally published at canro91.github.io

An Easy Mnemonic to Format Dates As Strings in C#

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.

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (2)

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield

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.

Collapse
 
canro91 profile image
Cesar Aguirre

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

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay