0Pricing
C# Academy · Lesson

Formatting Dates

Custom and standard formats.

Formatting Dates is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Format Dates

A raw DateTime prints using your machine's default culture, which may not be what you want.

Formatting lets you control exactly how a date appears: 2024-03-05, 05/03/2024, or Tuesday, March 5. C# gives you both standard and custom format strings.

ToString with a Pattern

The most flexible tool is ToString with a custom format string. Each letter stands for a part of the date.

For example, "yyyy-MM-dd" produces a year-month-day layout that sorts cleanly and is easy to read.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine(d.ToString("yyyy-MM-dd"));
    }
}

Year, Month, Day Letters

Format letters are case sensitive. yyyy is a four-digit year, MM is a two-digit month, and dd is a two-digit day.

Use uppercase M for month; lowercase m means minutes. Mixing them up is a common beginner bug.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine(d.ToString("dd/MM/yyyy"));
    }
}

Hours, Minutes, Seconds

For the time portion, HH is a 24-hour clock, mm is minutes, and ss is seconds.

Lowercase hh uses the 12-hour clock and pairs with tt for AM or PM.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
        Console.WriteLine(d.ToString("HH:mm:ss"));
    }
}

12-Hour Clock with AM/PM

To show a friendly 12-hour time, combine hh:mm with tt. The tt specifier prints AM or PM.

So 14:30 becomes 02:30 PM, which many readers find easier than a 24-hour value.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
        Console.WriteLine(d.ToString("hh:mm tt"));
    }
}

Month and Day Names

You can spell out names instead of numbers. MMMM gives the full month name and MMM gives a short one.

For weekdays, dddd is the full day name and ddd is the abbreviation.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine(d.ToString("dddd, MMMM dd"));
    }
}

Standard Format Specifiers

Besides custom patterns, C# offers short standard codes. "d" is a short date, "D" is a long date, "t" is a short time, and "T" is a long time.

These follow the current culture, so the exact look may vary by region.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
        Console.WriteLine(d.ToString("D"));
    }
}

Short and Full Helpers

There are convenience methods too. ToShortDateString and ToShortTimeString give brief output, while ToLongDateString spells things out.

They are quick, but for full control prefer an explicit format string.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine(d.ToShortDateString());
    }
}

Formatting in Interpolation

Inside an interpolated string you can place a format after a colon. Writing {d:yyyy-MM-dd} formats the value right where it appears.

This keeps your code short and readable when building messages.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine($"Date: {d:yyyy-MM-dd}");
    }
}

Culture-Invariant Output

For data you store or send between systems, format with CultureInfo.InvariantCulture so the result never depends on a user's regional settings.

This avoids surprises where the same code prints differently on another machine.

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5);
        Console.WriteLine(d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
    }
}

The Round-Trip Format

The standard "o" (round-trip) format outputs an ISO 8601 string that preserves every detail, including fractions of a second.

It is the safest choice when a date must be parsed back later without losing precision.

using System;

class Program
{
    static void Main()
    {
        DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
        Console.WriteLine(d.ToString("o"));
    }
}

Quick Check

You want a two-digit month inside a custom format string. Which specifier is correct?

Recap

You can format dates with ToString and custom patterns like yyyy, MM, dd, HH, and mm.

Standard codes such as "d" and "D" follow culture, while InvariantCulture and the "o" round-trip format keep output stable across systems.

Frequently asked questions

Is the “Formatting Dates” lesson free?

Yes — the full text of “Formatting Dates” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Formatting Dates”?

Custom and standard formats. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Formatting Dates” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating DateTimes
  2. Formatting Dates
  3. TimeSpan and Durations
  4. Date Math
← Back to C# Academy