DateTime.ToString Method (String)

Converts the value of the current DateTime object to its equivalent string representation, using a specified format.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

public string ToString (
         string format
)

Parameters

  • format
    A string that specifies the format to be used for the returned string.

Return Value

A string representation of the value of the current DateTime object, in the specified format.

Remarks

The format parameter should contain either a format specifier character or a custom format pattern.

If format is a null reference or an empty string, the general format specifier, 'G', is used.

The following table describes the standard DateTime format specifiers.

Format specifier Name Description
d Short date pattern

Represents a custom DateTime format string defined by the current ShortDatePattern property.

For example, the custom format string for the invariant culture is "MM/dd/yyyy".

D Long date pattern

Represents a custom DateTime format string defined by the current LongDatePattern property.

For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy".

f Full date/time pattern (short time) Represents a combination of the long date (D) and short time (t) patterns, separated by a space.
F Full date/time pattern (long time) Represents a custom DateTime format string defined by the current FullDateTimePattern property.
g General date/time pattern (short time) Represents a combination of the short date (d) and short time (t) patterns, separated by a space.
G General date/time pattern (long time) Represents a combination of the short date (d) and long time (T) patterns, separated by a space.
M or m Month day pattern

Represents a custom DateTime format string defined by the current MonthDayPattern property.

For example, the custom format string for the invariant culture is "MMMM dd".

s Sortable date/time pattern; conforms to ISO 8601 The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".
t Short time pattern

Represents a custom DateTime format string defined by the current ShortTimePattern property.

For example, the custom format string for the invariant culture is "HH:mm".

T Long time pattern Represents a custom DateTime format string defined by the current LongTimePattern property.
u Universal sortable date/time pattern The custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'".
U Universal sortable date/time pattern

Represents a custom DateTime format string defined by the current FullDateTimePattern property.

This pattern is the same as the full date/long time (F) pattern. However, formatting operates on the Coordinated Universal Time (UTC) that is equivalent to the DateTime object being formatted.

Y or y Year month pattern

Represents a custom DateTime format string defined by the current YearMonthPattern property.

For example, the custom format string for the invariant culture is "yyyy MMMM".

Version Information

Available in the .NET Micro Framework versions 2.0, 2.5, 3.0, 4.0, 4.1, and 4.2.

See Also

Reference

DateTime Structure
DateTime Members
System Namespace