Export (0) Print
Expand All

DateTime.ToString Method (String)

.NET Framework 1.1

Converts the value of this instance to its equivalent string representation using the specified format.

[Visual Basic]
Overloads Public Function ToString( _
   ByVal format As String _
) As String
[C#]
public string ToString(
 string format
);
[C++]
public: String* ToString(
 String* format
);
[JScript]
public function ToString(
   format : String
) : String;

Parameters

format
A format string.

Return Value

A string representation of value of this instance as specified by format.

Exceptions

Exception Type Condition
FormatException The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

format does not contain a valid custom format pattern.

Remarks

The format parameter should contain either a format specifier character or a custom format pattern. For more information, see the summary page for System.Globalization.DateTimeFormatInfo.

If format is a null reference (Nothing in Visual Basic) or an empty string, the general format specifier, 'G', is used.

This method uses formatting information derived from the current culture. For more information, see CurrentCulture.

Example

The following sample demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Globalization

Public Class MainClass
    
    Public Shared Sub Main()
        Dim dt As DateTime = DateTime.Now
        Dim myformat() As String =  {"d", "D", _
                                    "f", "F", _
                                    "g", "G", _
                                    "m", _
                                    "r", _
                                    "s", _
                                    "t", "T", _
                                    "u", "U", _
                                    "y", _
                                    "dddd, MMMM dd yyyy", _
                                    "ddd, MMM d ""'""yy", _
                                    "dddd, MMMM dd", _
                                    "M/yy", _
                                    "dd-MM-yy"}
        Dim mydate As String
        Dim i As Integer
        For i = 0 To myformat.Length - 1
            mydate = dt.ToString(myformat(i), DateTimeFormatInfo.InvariantInfo)
            Console.WriteLine(String.Concat(myformat(i), " :", mydate))
        Next i

    ' Output.
    '
    ' d :08/17/2000
    ' D :Thursday, August 17, 2000
    ' f :Thursday, August 17, 2000 16:32
    ' F :Thursday, August 17, 2000 16:32:32
    ' g :08/17/2000 16:32
    ' G :08/17/2000 16:32:32
    ' m :August 17
    ' r :Thu, 17 Aug 2000 23:32:32 GMT
    ' s :2000-08-17T16:32:32
    ' t :16:32
    ' T :16:32:32
    ' u :2000-08-17 23:32:32Z
    ' U :Thursday, August 17, 2000 23:32:32
    ' y :August, 2000
    ' dddd, MMMM dd yyyy :Thursday, August 17 2000
    ' ddd, MMM d "'"yy :Thu, Aug 17 '00
    ' dddd, MMMM dd :Thursday, August 17
    ' M/yy :8/00
    ' dd-MM-yy :17-08-00
    End Sub 'Main 
End Class 'MainClass

[C#] 
using System;
using System.Globalization; 

public class MainClass { 
   public static void Main(string[] args)  {
       DateTime dt = DateTime.Now;
       String[] format = {
           "d", "D",
           "f", "F",
           "g", "G",
           "m",
           "r",
           "s",
           "t", "T",
           "u", "U",
           "y",
           "dddd, MMMM dd yyyy",
           "ddd, MMM d \"'\"yy",
           "dddd, MMMM dd",
           "M/yy",
           "dd-MM-yy",
       };
       String date;
       for (int i = 0; i < format.Length; i++) {
           date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
           Console.WriteLine(String.Concat(format[i], " :" , date));
       }
 
  /** Output.
   *
   * d :08/17/2000
   * D :Thursday, August 17, 2000
   * f :Thursday, August 17, 2000 16:32
   * F :Thursday, August 17, 2000 16:32:32
   * g :08/17/2000 16:32
   * G :08/17/2000 16:32:32
   * m :August 17
   * r :Thu, 17 Aug 2000 23:32:32 GMT
   * s :2000-08-17T16:32:32
   * t :16:32
   * T :16:32:32
   * u :2000-08-17 23:32:32Z
   * U :Thursday, August 17, 2000 23:32:32
   * y :August, 2000
   * dddd, MMMM dd yyyy :Thursday, August 17 2000
   * ddd, MMM d "'"yy :Thu, Aug 17 '00
   * dddd, MMMM dd :Thursday, August 17
   * M/yy :8/00
   * dd-MM-yy :17-08-00
   */
   }
}

[C++] 
#using <mscorlib.dll>
 
 using namespace System;
 using namespace System::Globalization; 
 
 void main ()
 {
   DateTime dt = DateTime::Now;
   String* format[] = {
     L"d", L"D",
     L"f", L"F",
     L"g", L"G",
     L"m",
     L"r",
     L"s",
     L"t", L"T",
     L"u", L"U",
     L"y",
     L"dddd, MMMM dd yyyy",
     L"ddd, MMM d \"'\"yy",
     L"dddd, MMMM dd",
     L"M/yy",
     L"dd-MM-yy",
     };
   String * date;
   for (int i = 0; i < format->Length; i++)
   {
     date = dt.ToString(format[i], DateTimeFormatInfo::InvariantInfo);
     Console::WriteLine(String::Concat(format[i], L" :" , date));
   }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
 }

[JScript] 
import System;
import System.Globalization; 
 

var dt : DateTime = DateTime.Now;
var format : String[] = [
    "d", "D",
    "f", "F",
    "g", "G",
    "m",
    "r",
    "s",
    "t", "T",
    "u", "U",
    "y",
    "dddd, MMMM dd yyyy",
    "ddd, MMM d \"'\"yy",
    "dddd, MMMM dd",
    "M/yy",
    "dd-MM-yy",
    ];
var date : String;
for (var i : int = 0; i < format.Length; i++) {
    date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
    Console.WriteLine(format[i] + " :" + date);
}

  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace | DateTime.ToString Overload List | Formatting Overview | Date and Time Format Strings | DateTimeFormatInfo | CultureInfo

Show:
© 2014 Microsoft