0Pricing
C# Academy · Lesson

Formatting Dates

Custom and standard formats.

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"));
    }
}

All lessons in this course

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