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
Call
What 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
Parameter
Description
format
Optional. A standard letter (like "D") or a custom pattern (like "yyyy-MM-dd").
provider
Optional. A CultureInfo that supplies month names, day names, and separators.
Return Value
Return Value
Description
string
The 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));
}
}
Output
2026-11-18
14:30
Wednesday, November 18
Nov 18, 2026
How It Works
Year-first.yyyy-MM-dd is easy to sort and read in any country.
24-hour clock.HH:mm gives hours and minutes with no seconds.
Names.dddd is the full weekday and MMMM is the full month name.
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"));
}
}
Output
11/18/2026 14:30:45
Wednesday, 18 November 2026
14:30:45
2026-11-18T14:30:45
How It Works
Fixed culture. Setting CurrentCulture to invariant makes the first line the same everywhere.
No argument.ToString() is the same as the "G" format: short date plus long time.
D and T.D is the long date and T is the long time.
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));
}
}
Output
mercredi 18 novembre 2026
novembre
Mittwoch
Mittwoch, 18. November 2026
How It Works
Two culture objects.fr-FR is French as used in France and de-DE is German as used in Germany.
Translated names. The weekday and month names come from the culture, not from your code.
Different layout. German adds a comma and a dot after the day number, French does not.
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.