C# DateTime ToString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.ToString converts a date and time into text. With no argument it uses the current culture's general format. With a format string you choose exactly how the text looks.

You can use a custom pattern such as yyyy-MM-dd or a standard letter such as D (long date) or s (sortable). Pass a CultureInfo to control month names and separators.

This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.ToString(...). DateTime is a struct, so every value is copied and never changed in place.

Syntax

Call it with an optional format and an optional culture.

C#
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
string a = dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);   // 2026-11-18
string b = dt.ToString("HH:mm", CultureInfo.InvariantCulture);        // 14:30
string c = dt.ToString("D", new CultureInfo("fr-FR"));                // long date in French
CallWhat it does
dt.ToString()General short format of the current culture.
dt.ToString("yyyy-MM-dd HH:mm:ss", culture)Custom pattern, same text on every PC with the invariant culture.
dt.ToString("D", culture)A standard format letter.

Common pattern letters: yyyy year, MM month number, MMM short month name, dd day, HH 24-hour, hh 12-hour, mm minutes, ss seconds, fff milliseconds, tt AM/PM.

Library declarations
C#
public override string ToString();
public string ToString(string? format);
public string ToString(IFormatProvider? provider);
public string ToString(string? format, IFormatProvider? provider);

Parameters

ParameterDescription
formatOptional. A standard letter (like "D") or a custom pattern (like "yyyy-MM-dd").
providerOptional. A CultureInfo that supplies month names, day names, and separators.

Return Value

Return ValueDescription
stringThe formatted date and time.

C# Examples

Example 1 — Custom Format Strings

Print the same date in four different custom patterns.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        CultureInfo inv = CultureInfo.InvariantCulture;
        Console.WriteLine(dt.ToString("yyyy-MM-dd", inv));
        Console.WriteLine(dt.ToString("HH:mm", inv));
        Console.WriteLine(dt.ToString("dddd, MMMM d", inv));
        Console.WriteLine(dt.ToString("MMM dd, yyyy", inv));
    }
}

How It Works

  1. Year-first. yyyy-MM-dd is easy to sort and read in any country.
  2. 24-hour clock. HH:mm gives hours and minutes with no seconds.
  3. Names. dddd is the full weekday and MMMM is the full month name.
  4. Short month. MMM prints Nov, and d or dd controls the leading zero of the day.

Example 2 — Default and Standard Formats

Set the current culture to invariant and compare ToString() with standard letters.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        Console.WriteLine(dt.ToString());
        Console.WriteLine(dt.ToString("D"));
        Console.WriteLine(dt.ToString("T"));
        Console.WriteLine(dt.ToString("s"));
    }
}

How It Works

  1. Fixed culture. Setting CurrentCulture to invariant makes the first line the same everywhere.
  2. No argument. ToString() is the same as the "G" format: short date plus long time.
  3. D and T. D is the long date and T is the long time.
  4. s is sortable. It writes yyyy-MM-ddTHH:mm:ss, which sorts correctly as plain text.

Example 3 — Month and Day Names in Other Languages

Format the same date for French and German readers.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        var fr = new CultureInfo("fr-FR");
        var de = new CultureInfo("de-DE");
        Console.WriteLine(dt.ToString("D", fr));
        Console.WriteLine(dt.ToString("MMMM", fr));
        Console.WriteLine(dt.ToString("dddd", de));
        Console.WriteLine(dt.ToString("D", de));
    }
}

How It Works

  1. Two culture objects. fr-FR is French as used in France and de-DE is German as used in Germany.
  2. Translated names. The weekday and month names come from the culture, not from your code.
  3. Different layout. German adds a comma and a dot after the day number, French does not.
  4. Needs culture data. These names rely on culture data being available; it is on by default on Windows and most Linux setups.

Frequently Asked Questions

MM is the month number. mm is the minutes. Mixing them up is a common bug.
Use HH in the format, for example HH:mm:ss. Use hh with tt for a 12-hour clock.
It uses the current culture. Pass CultureInfo.InvariantCulture to get stable text.
Use "o" (round-trip) or "s" (sortable) with the invariant culture, or store the value as a DateTime in a database.
It is the "G" format: a short date followed by a long time, in the current culture.

Next: ToUniversalTime()

Convert local time to UTC; the result has Kind Utc with System.DateTime.

ToUniversalTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful